$ .getJSON not working

I am looking for a related topic in jQuery. But I did not see any way to solve my problem.

$(document).ready(function(){ $("#inputForm").submit(function(event){ $(":text").each(function() { var inputText = $(this).val(); var userList = []; var weblink = 'http://test.com'; // problem is from here. $.getJSON(weblink, function(data){ alert(weblink); // this statement doesn't show up $.each(data, function(entryIndex, entry){ userList.push(entry['from_user']); }); }); alert(userList); }); }); }); 

There are four problems here:

  • Why the first warning ('weblink') is not displayed.
  • Why this code cannot get json data from the website.
  • The purpose of this code is to get the from_user tag from the json file and save it in the userList array.
  • Variables in the variable $ .each (data, function (entryIndex, entry) {", the function has two input parameters: one is entryIndex and the other is record, I am so surprised that these two parameters are for what? Can I use these two parafile.

Can anyone help me solve this problem. I was here for one day. Thank you very much.

+7
source share
5 answers

There are several problems:

  • getJSON executes an ajax request. Ajax requests are subject to policies of the same origin . If your page is not loaded from http://test.com (or several other caveats), it will not work. You are probably looking for JSON-P (which also supports jQuery) if the server supports it.

  • getJSON , like all ajax requests, is asynchronous by default, so your second warning (with a list of users) will occur before the request completes. Although you can make ajax requests synchronous, this is a very bad idea (it blocks the user interface of most browsers during the request). Instead, just use the user list after you got it in the callback, instead of trying to use it in the function that calls getJSON .

Change You said below that you are trying to use the Twitter Search API. This API supports JSON-P, so if you use JSON-P to fulfill your request, it should work. eg:.

 $(document).ready(function(){ $("#inputForm").submit(function(event){ $(":text").each(function() { var inputText = $(this).val(); var userList = []; var weblink = 'http://search.twitter.com/search.json?q=&ands=google'; // problem is from here. $.ajax({ url: weblink, dataType: "jsonp", // <== JSON-P request success: function(data){ alert(weblink); // this statement doesn't show up $.each(data.result, function(entryIndex, entry){ // <=== Note, `data.results`, not just `data` userList.push(entry['from_user']); // <=== Or `entry.from_user` would also work (although `entry['from_user']` is just fine) }); alert(userList); // <== Note I've moved this (see #2 above) } }); }); }); }); 

... but you do not want to do this for each text field in the form?

Here is a live example , but without a form (and only one request, not a request for each field).

+14
source

Just add a link

 &callback=? 

or

 ?callback=? 

(if this is the first and only GET variable) This will call your call into a JSONP call that has no problems with policies of the same origin.

+4
source

Not sure, but does it require arguments? Try the following:

 $.getJSON(weblink, {}, function(data){ 
+1
source

Do you use the MVC platform? Apparently, by default, MVC 2.0 blocks GET requests for actions that return JsonResult. Departure:

http://mhinze.com/2010/04/13/json-hijacking-in-asp-net-mvc-2/

I had the same problem when updating an existing application (written in MVC v1) that used .getJSON calls to retrieve data from MVC Controller in View (via jquery / javascript) and could not understand why they did not work. I tried different versions of jquery with no luck, then I found the link above. Instead, I changed it to POST (and also slightly changed how the data was sent from the Controller - the Dictionary is used) and made it work - finally !!

Good luck Jayd

+1
source

I would like to add another reason why jQuery.getJSON () might not respond as expected in the development environment in IIS for Windows.

Following all the procedures given here, I could not get the data from the * .json file only if I changed the extension to * .js, the response status is returned

 {"status"="404", "statusText"="Not Found"} 

Then I remembered that with IIS there are some MIME / types that are not specified. Adding a MIME / Type / json application for files with the .json extension solved my problem.

Information on how to add MIME / types on iis7.

0
source

All Articles