简体   繁体   English

如何使用jquery ajax和jsonp在您自己的域上读取json数据

[英]How to read json data on your own domain using jquery ajax and jsonp

I have this code, and I want to read the JSON data on my domain, but i've tried all I could think about, but could not get it. 我有这段代码,我想在我的域上读取JSON数据,但是我已经尽力想了一下,但没有得到。

This is the code: 这是代码:

<script>
    $(function () {
        $.ajax({
            "url": "http://www.mmsbip.com.ng/accounts.json",
            "type": "get",
            "dataType": "json"
        })
        .done(function (data) {
            var options = $("#users");
            $.each(data, function (val) {
                   alert(data.UserID);   
                }));
            });
        })
        .fail(function (jqXHR, status, error) {
            console.log("status:", status, "error:", error);
        })
    });

And the data on my server in the accounts.json is 我账户中的服务器上的数据是

[
{
    "UserID": "timotech",
    "UserName": "timotech@yahoo.com",
    "Password": "password"
},
{
    "UserID": "teslim",
    "UserName": "teslimbakare@live.com",
    "Password": "password"
},
{
    "UserID": "bola",
    "UserName": "bettyolabode@yahoo.com",
    "Password": "password"
}

] ]

How do I get this data from the server 如何从服务器获取此数据

Edit: I decided to post the whole page so that you can see where i'm making any mistakes. 编辑:我决定发布整个页面,以便您可以看到我在哪里犯任何错误。 Thanks 谢谢

 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Simple JSON Example</title> <script src="jmobile/jquery-2.1.3.js"></script> <script> $(function () { $.ajax({ "url": "http://www.mmsbip.com.ng/accounts.json", "type": "get", "dataType": "json" }) .done(function (data) { var options = $("#users"); $.each(data, function (val) { alert(data.UserID); }); }) .fail(function (jqXHR, status, error) { console.log("status:", status, "error:", error); }) }); </script> </head> <body> <h2>Simple JSON Example</h2> <p>This DropDown is populated using $.ajax()</p> <select name="users" id="users" /> </body> </html> 

The alert is just to test whether the json data is being returned. 警报只是为了测试是否返回json数据。 Thanks 谢谢

There are a couple of small errors in the code. 代码中有几个小错误。 Try this instead: 尝试以下方法:

    $(function () {
        $.ajax({
            "url": "http://www.mmsbip.com.ng/accounts.json",
            "type": "get",
           "dataType": "json"
     })
    .done(function (data) {
        var options = $("#users");
        $.each(data, function (val) {
           alert(data.UserID);   
        });
    })
    .fail(function (jqXHR, status, error) {
       console.log("status:", status, "error:", error);
    })
});

You had closed the original function too soon (after the alert) and had an extra }); 您太早(在警报之后)关闭了原始功能,并有一个额外的}); in the code. 在代码中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM