开发者

jQuery XHR question

开发者 https://www.devze.com 2023-03-21 06:49 出处:网络
I\'m trying to wr开发者_StackOverflow社区ite up a simple jquery script that parses some JSON and formats it. Nothing too special, but I keep getting the following error in Chrome: Resource interpreted

I'm trying to wr开发者_StackOverflow社区ite up a simple jquery script that parses some JSON and formats it. Nothing too special, but I keep getting the following error in Chrome: Resource interpreted as Script but transferred with MIME type text/html.

I've looked it up on SO and it looks like a fairly common question. I've followed every piece of advice I can find and still no luck yet. Here's my code:

var url = "http://somesite.com/something/json/json_index.php?q=";
var query = "someQuery";

$.ajax({
  url: url + query + "&callback=",
  crossDomain:true, 
  dataType: 'jsonp',
  success: function(data){
    alert('all done!');
  },
  error: function(data){
    alert('fail.');
  }
});

Anyone have any ideas/suggestions?


JSON-P works by creating a <script> tag in your HTML document, and the remote server is expected to return yourCallback({the: data}). The error message sounds to me like the server is returning an HTML document instead, which could be a 404 error or something along those lines, or could just be the result of a misconfigured server (though that sounds unlikely if this is an API that's been tested for JSON-P).

Google Chrome's Developer Tools also has a Network tab, so I'd recommend checking what request is being sent and what response you're receiving. This sounds like something that we assorted StackOverflow users can't exactly debug without knowing more about exactly what API you're using, but you should hopefully be able to debug it with the tools you have :)

I also note that you're sending a URL ending with callback=, but the jQuery docs say you should use callback=?, with a question mark, so that jQuery can fill in a callback name. Maybe adding the ? is all you need.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号