1

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!

Bato Dor
  • 841
  • 3
  • 11
  • 33
  • Possibly related: [json Uncaught SyntaxError: Unexpected token :](http://stackoverflow.com/questions/7936610/json-uncaught-syntaxerror-unexpected-token) – megawac Oct 22 '13 at 14:14
  • @megawac if i change the `dataType` to `json` then i get a mistake: `XMLHttpRequest cannot load` – Bato Dor Oct 22 '13 at 14:17
  • Whats response are you getting from the server – megawac Oct 22 '13 at 14:20
  • @megawac do u mean this? 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 – Bato Dor Oct 22 '13 at 14:23
  • Alright can you add a preview of the response in your question – megawac Oct 22 '13 at 14:26
  • Sorry, I meant the data response - it should look something like `jQuery17108861840174067765_1382451880471(data)` can you verify this – megawac Oct 22 '13 at 14:32
  • @megawac here it is: `callback:jQuery182007281651743687689_1382452295311 _:1382452295320` – Bato Dor Oct 22 '13 at 14:33
  • You have to set `jsonp` headers in the requested file to debug `Resource interpreted as Script but transferred with MIME type text/html` – idmean Oct 22 '13 at 14:41
  • @wumm u mean to set instead of `jsonp` to `json` if so, i have a mistake `XMLHttpRequest cannot load` or what should i do exactly? ) – Bato Dor Oct 22 '13 at 14:43
  • @venom Oh no that was a typo. I meant to set the content header just to jsonp because in your response headers there it says: `Content-Type: text/html;` and that's the reason for the second error message. – idmean Oct 22 '13 at 14:44
  • @wumm sorry, i didnt understand ) can u be more specific? – Bato Dor Oct 22 '13 at 14:48
  • Sorry, I told you some (in that case) useless things. You can't do ajax calls JSONP calls on sites which are not made for that. – idmean Oct 22 '13 at 14:55
  • @wumm i see so u mean i cant get data from my website unless i let the cross-browsing on it? – Bato Dor Oct 22 '13 at 14:57
  • I think so, but I'm haven't often worked with JSONP, maybe there's a way. – idmean Oct 22 '13 at 14:59
  • Are you trying to call json on another domain? Because it might be a problem of cross-origin resource sharing (https://developer.mozilla.org/en/docs/HTTP/Access_control_CORS) – Pith Oct 22 '13 at 15:13
  • @pith i know, that's why im using `JSONP` instead of `JSON` – Bato Dor Oct 22 '13 at 15:17

1 Answers1

2

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.

Community
  • 1
  • 1
senordev
  • 428
  • 2
  • 5