How to use $.getJSON() properly?
I'm having really strange problem with $.getJSON()
on localhost. My test code is below:
$(document).ready(function(){
var url = "http://script.mydomain.com/location/newid"
var url2 = "http://localhost/cms/location/newid"
$.getJSON(url2, function(id) {
alert(id);
});
});
For url2 it works perfect but for url it's not working. Could anyone give some hint to solve this problem? These urls returns json (example: "34") if anyone would like to know.
EDIT [SOLVED]:
If anyone in future will use $.getJSON
remember that you can't call "alien" domains.
When I executed over开发者_开发知识库head script under script.mydomain.com
domain everything works fine!
AJAX request are limited by a cross-domain policy. Basically, you can't do ajax requests if they're not going to the server the original site was hosted on. It's slightly more complicated then that, but I would assume this to be the case.
You are limited by browser security to obtaining code from your own website (jquery uses XMLHttpRequest for getJSON). To get around it, you need to use jsonp (jquery adds a script tag) or create and append a script tag to your document.
Chances are you have a problem that you are trying to run in a browser ajax-request from the local host, to the domain cw.uppercut.pl.
This is not an issue of the library.
精彩评论