$ .getJSON不起作用 [英] $.getJSON not working

查看:96
本文介绍了$ .getJSON不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jQuery中搜索相关主题.但是我没有找到解决问题的任何方法.

I search related topic in jQuery. But I didn't saw any method 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);
        });
     });
});

这里有四个问题:

  1. 为什么第一个警报(网络链接")没有显示.
  2. 为什么此代码无法从网站获取json数据
  3. 此代码的目标是从json文件中获取from_user标记,并将其存储到userList数组中.
  4. "$.each(data,function(entryIndex,entry){"语句中的变量,该函数有两个输入参数,一个是entryIndex,另一个是entry,我很奇怪这两个参数是做什么用的,以及如何使用我可以使用这两个参数吗?

任何人都可以帮助我解决此问题.我已经在这里待了一天. 非常感谢.

Can anyone help me solve this problem. I have been stock in here for one day. Thank you very much.

推荐答案

那里有两个问题:

  1. getJSON进行ajax请求. Ajax请求必须遵守相同来源政策.除非您的页面是从http://test.com(或其他几个警告)中加载的,否则它将无法正常工作.您可能正在寻找 JSON-P (jQuery也支持),前提是该服务器支持它.

  1. getJSON does an ajax request. Ajax requests are subject to the Same Origin Policy. Unless your page is loaded from http://test.com (or a couple of other caveats), it won't work. You're probably looking for JSON-P (which jQuery also supports), provided the server supports it.

getJSON与所有ajax请求一样,默认情况下为异步,因此您的第二次警报(带有用户列表)将在请求完成之前 .尽管您可以使ajax请求同步,但这是一个非常糟糕的主意(在请求期间锁定大多数浏览器的UI).相反,只需在回调中收到用户列表后再使用它,而不要尝试在调用getJSON的函数中使用它.

getJSON, like all ajax requests, is asynchronous by default, so your second alert (with the user list) will happen before 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 getJSON.

编辑:您在下面说过,您正在尝试使用Twitter搜索API.该API 确实支持JSON-P,因此,如果您使用JSON-P进行请求,它应该可以工作.例如:

Edit: You've said below that you're trying to use the Twitter search API. That API does support JSON-P, so if you use JSON-P to do your request, it should work. e.g.:

$(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 surely you don't want to do that for each text field in the form?

这是一个实时示例,但没有表单(并且仅执行一个请求,而不是每个字段的请求)

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

这篇关于$ .getJSON不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆