Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A couple of issues there:</p> <ol> <li><p><code>getJSON</code> does an ajax request. Ajax requests are subject to the <a href="http://en.wikipedia.org/wiki/Same_origin_policy" rel="noreferrer">Same Origin Policy</a>. Unless your page is loaded from <code>http://test.com</code> (or a couple of other caveats), it won't work. You're probably looking for <a href="http://en.wikipedia.org/wiki/JSONP#JSONP" rel="noreferrer">JSON-P</a> (which jQuery also supports), provided the server supports it.</p></li> <li><p><code>getJSON</code>, like all ajax requests, is <em>asynchronous</em> by default, so your second alert (with the user list) will happen <em>before</em> the request completes. Although you can make ajax requests synchronous, it's a very bad idea (locks up the UI of most browsers during the request). Instead, just use the user list after you've received it in the callback, rather than trying to use it in the function calling <code>getJSON</code>.</p></li> </ol> <p><strong>Edit</strong>: You've said below that you're trying to use the Twitter search API. That API <strong>does</strong> support JSON-P, so if you use JSON-P to do your request, it should work. e.g.:</p> <pre><code>$(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=&amp;ands=google'; // problem is from here. $.ajax({ url: weblink, dataType: "jsonp", // &lt;== JSON-P request success: function(data){ alert(weblink); // this statement doesn't show up $.each(data.result, function(entryIndex, entry){ // &lt;=== Note, `data.results`, not just `data` userList.push(entry['from_user']); // &lt;=== Or `entry.from_user` would also work (although `entry['from_user']` is just fine) }); alert(userList); // &lt;== Note I've moved this (see #2 above) } }); }); }); }); </code></pre> <p>...but surely you don't want to do that for <em>each</em> text field in the form?</p> <p><a href="http://jsbin.com/ihija5" rel="noreferrer">Here's a live example</a> but without a form (and only doing one request, not a request for each field).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload