jQuery getJSON 调用未返回所需的参数?

发布于 2024-11-07 21:29:57 字数 960 浏览 0 评论 0原文

我在 jQuery 中搜索了相关主题,但没有看到任何方法来解决我的问题。

$(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);
        });
     });
});

这里有3个问题:

  1. 为什么第一个alert('weblink')没有出现?
  2. 为什么这段代码无法从网站获取json数据?
  3. 此代码的目标是从 json 文件获取 from_user 标记并将其存储到 userList 数组中。

“$.each(data, function(entryIndex,entry){”语句中的变量,该函数有两个输入参数,一个是entryIndex,另一个是entry。我想知道这些参数的用途以及如何使用它们?

可以谁能帮我解决这个问题,我已经在这里呆了一天了。 非常感谢。

I searched for a related topic in jQuery, but I didn't see 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);
        });
     });
});

There are 3 problems here:

  1. Why doesn't the first alert('weblink') doesn't show up?
  2. Why this code can't get the json data from website?
  3. The goal of this code is to get the from_user tag from json file and store into userList array.

The variables in "$.each(data, function(entryIndex, entry){" statement, the function have two input parameters one is entryIndex and other is entry. I am wondering what these parameters are for and how to use them?.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我只土不豪 2024-11-14 21:29:58

只需添加到链接

&callback=?

?callback=?

(如果它是第一个且唯一的 GET 变量)
这将使您的调用转为 JSONP 调用,这不会出现同源策略问题。

Just add to link

&callback=?

or

?callback=?

(if it's first and only GET variable)
This will make your call into JSONP call, which doesn't have problems with Same Origin Policy.

不确定,但它不需要获取参数吗?试试这个:

$.getJSON(weblink, {}, function(data){

Not sure but doesn't it require get arguments? Try this:

$.getJSON(weblink, {}, function(data){
儭儭莪哋寶赑 2024-11-14 21:29:58

你使用MVC平台吗?
显然,默认情况下,MVC 2.0 会阻止对返回 JsonResult 的操作的 GET 请求。
查看:

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

我在升级使用 .getJSON 调用的现有应用程序(用 MVC v1 编写)时遇到了同样的问题将数据从 MVC 控制器提取到视图(通过 jquery/javascript)&无法弄清楚为什么它们不起作用我尝试了不同版本的 jquery 但没有成功,然后我找到了上面的链接。我改为 POST (并且还稍微更改了从控制器发送数据的方式 - 使用 Dictionary )并使其正常工作 - 终于!

祝你好运!
杰伊德

Are you using MVC platform?
Apparently, by default MVC 2.0 blocks GET requests to actions that return a JsonResult.
Check out:

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

I had the same problem whilst upgrading an existing application (written in MVC v1) that used .getJSON calls to pull data from MVC Controller to View (via jquery/javascript) & could not figure out why they did not work I tried different versions of jquery with no luck, then I found the above link. I changed to POST instead (and also changed slightly how the data was sent from the Controller - used Dictionary ) and got it working - finally!!

Good luck!
JayD

心舞飞扬 2024-11-14 21:29:58

我想补充一下为什么 jQuery.getJSON() 在 Windows 的 IIS 开发环境中可能无法按预期响应的另一个原因。

按照此处提供的所有过程,我无法从 *.json 文件中获取任何数据,只有当我将扩展名更改为 *.js 时,才会返回响应状态

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

然后我记得在 IIS 中,有一些 MIME/类型没有指定。为扩展名为 .json 的文件添加 MIME/Type application/json 解决了我的问题。

有关如何在 iis7 上添加 MIME/类型的信息。

I would like to add another reason why jQuery.getJSON() might not response as expected in a developing environment under IIS for Windows.

Following all the procedures provided here, I was not able to get any data from a *.json file, only if I changed the extension to *.js, the response status returned

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

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

For information how to add MIME/types on iis7.

蔚蓝源自深海 2024-11-14 21:29:57

这里有几个问题:

  1. getJSON 执行 ajax 请求。 Ajax 请求须遵守同源政策。除非您的页面是从 http://test.com 加载的(或其他一些注意事项),否则它将无法工作。您可能正在寻找 JSON-P (jQuery 也支持),前提是服务器支持它。

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

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

$(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)
                }
            });
        });
     });
});

...但是您肯定不想对表单中的每个文本字段执行此操作吗?

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

A couple of issues there:

  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.

  2. 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.

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文