I'm a beginner in jQuery, and I tried to use the getJSON, but I always have an error when using it with an URL, whereas it worked when I used it with a text file. Can you please tell me what is wrong with the URL ?
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$.getJSON("http://localhost/edoc?appname=eDRITS&prgname=JSON2",function(item){
alert("success");
$.each(item.users, function(i,user){
content = '<p>' + user.name + ', ' + user.fname + '</p>';
$(content).appendTo("#list");
});
})
.success(function() { alert("second success"); })
.error(function() { alert("error"); });
});
</script>
The URL in the code is the call to a program which returns JSON data :
开发者_运维百科{"users":[ {"usrid":"GABR90","name":"ABRAMOVA ","fname":"Galina "}] }
If I replace the URL by a text file which contains the JSON data, it works :
$.getJSON("file.txt",function(item){...}
The error I get with the URL is just the display of my ".error() function", I dont have any specific error message, and I don't know how to use debugger to see the HTTP Get of the browser (which is Firefox).
So how can I use it with the URL, please ?
Thanks for your help
Are you accessing the HTML page using file:/// and then have that page call http://localhost ? If that is the case, then you are hitting the "same origin" limitation.
Your browser see the page as coming from a file:// url, and when that page tries to connect to http://localhost, that is technically another domain, and for security reasons the browser may (and usually will) block that communication. When you instead point to a text file, it see both the html and the file coming from file:// and will allow the communication.
You should load the HTML page in your web server, so that you can access the page using a http://localhost/mypage.html or similar url.
精彩评论