Search code examples
jqueryajaxjsoncross-browserjsonp

jsonp crossbrowser mistakes from domain or code side


im newbie in api and json getting data so i have a question regarding of getting the information by using jsonp from my website: http://pda.bilgiteknolojileri.net/ i know that the domain name is pda not the api :) this is not my mistake though, not my domains, but i can moderate them, anyway i have a code:

    var url='http://pda.bilgiteknolojileri.net';
    $.ajax({
        type: "GET",
        url: url,
        dataType: 'jsonp',
        jsonp: 'jsonp',
        crossDomain: true,
        success: function(cats) {
            $.each(cats.data, function(i, data) {
                var cat=data.cat;
                var cat_id=data.cat_id;
                $('#category').append('<option value="'+cat_id+'">'+cat+'</option>');
            });
        }
    });

as far as i understand the code is ok, but there is a mistake in console: Uncaught SyntaxError: Unexpected token < and second not big mistake Resource interpreted as Script but transferred with MIME type text/html what im doing wrong? is it the cross-browser mistake from the domain side or mine in code?

Request Headers:

GET /?callback=jQuery182007281651743687689_1382452295311&_=1382452295320 HTTP/1.1
Host: pda.bilgiteknolojileri.net
Connection: keep-alive
Cache-Control: no-cache
Pragma: no-cache
Accept: */*
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
DNT: 1
Accept-Encoding: gzip,deflate,sdch
Accept-Language: tr,en-US;q=0.8,en;q=0.6,ru;q=0.4
Cookie: fbm_224237501004640=base_domain=.bilgiteknolojileri.net; CFID=337971; CFTOKEN=5130db21daca4ae5-BE9FEE21-0FA8-399A-7485246A32933BCE; JSESSIONID=7030f7b10f91d2d2d0ce10456f5c7a302350; WRK_COOKIE_ID=BE9FF125%2DA082%2D5206%2DD01747735EADD58B; __utma=56243896.53108638.1381244578.1382443118.1382450419.21; __utmc=56243896; __utmz=56243896.1381244578.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)

Query string parameters

callback:jQuery182007281651743687689_1382452295311
_:1382452295320

Response headers:

HTTP/1.1 200 OK
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Tue, 22 Oct 2013 14:19:24 GMT

The problem is that the response is in text/html format, where it should be javascript/application, what should i change? i cant get any data at all... thank you for the help!


Solution

  • There are two issues here:

    1. The server response header is claiming a mime-type of text/html
    2. The server isn't responding with a JSONP object. This is the biggest issue.

    The server is wrapping your JSON object with HTML. If you look at the response body from: http://pda.bilgiteknolojileri.net/?callback=jQuery182007281651743687689_1382452295311&_=1382452295320

    You'll see that it starts with:

    <html>
        <head><meta ...
    

    That first < is what's causing the unexpected token error.

    Your server needs to respond with something more like this:

    jQuery182007281651743687689_1382452295311({...JSON OBJECT...})
    

    A little more explanation can be found here: Confused on how a JSONP request works

    It was mentioned before, but your question is almost a duplicate to this one: json Uncaught SyntaxError: Unexpected token :

    The difference being that instead of just a JSON object being sent over, an html document with the JSON INSIDE it was sent over.