0

我从 2 天开始就遇到了这个问题,我需要使用 javascript 从下面的 url 获取数据。

http://www.apexweb.co.in/Apex_Quote/Download_Data.asp?un=9393910169

我解析了静态数据,但我需要解析来自 url 的数据。帮我

var employees = [{"ContactName":"AksharmaDelhi","ContactNumber":"9313146690","MainCategory":"NONLOCAL","SubCategory":"LIG"},
{"ContactName":"AbhijeetPune","ContactNumber":"9271263359","MainCategory":"NONLOCAL","SubCategory":"LIG"},
{"ContactName":"Abhinandan","ContactNumber":"9954534007","MainCategory":"NONLOCAL","SubCategory":"LIG"},
{"ContactName":"Abhishek","ContactNumber":"9302172932","MainCategory":"NONLOCAL","SubCategory":"LIG"},
{"ContactName":"AbhishekPuri","ContactNumber":"9303928233","MainCategory":"NONLOCAL","SubCategory":"LIG"},
{"ContactName":"AbhishekZhakkas","ContactNumber":"9863027167","MainCategory":"NONLOCAL","SubCategory":"MIG"},
{"ContactName":"Abin","ContactNumber":"8891886340","MainCategory":"NONLOCAL","SubCategory":"MIG"},
{"ContactName":"AccountantSrinivas","ContactNumber":"9949861074","MainCategory":"LOCAL","SubCategory":"MIG"},];

alert(employees.length);   //get length 

for(var i = 0;i<employees.length;i++){

alert(employees[i].ContactName + employees[i].ContactNumber + employees[i].MainCategory + employees[i].SubCategory);

}
</script>
4

2 回答 2

1

有很多方法可以做到这一点,我使用$.ajax以下方式:

$.ajax({
            url : "http://www.apexweb.co.in/Apex_Quote/Download_Data.asp?un=9393910169",
            type : "get",
            dataType : 'text',
            success : function(data) {                                  
                var employees = $.parseJSON(data);
                for(var i = 0;i<employees.length;i++){
                alert(employees[i].ContactName + employees[i].ContactNumber + employees[i].MainCategory + employees[i].SubCategory);
                }                       
            },
            error : function() {
                //console.log("your call failed");
                }                                   
        });

从您的 URL 获取的数据将作为回调中的data参数传递。success$.getJson相反,这让我可以更好地控制我得到的东西。

在这种方法中,我将 dataType 设置为text. 这意味着 jquery 不会尝试对我的结果做任何“聪明”的事情。如果我已将其设置为参数,则jsondata参数将被解析并准备好使用。您不需要手动解析它:

var employees = $.parseJSON(data);

于 2013-05-20T13:11:54.443 回答
0

对于跨域 ajax,我建议使用 $.getJSON()

$.getJSON("http://my-site-return-json.com/index.php?", function(data){
  // Load Finish
});

在服务器端

记得返回回调

$callback = $_GET['callback'];
$arr = array("a" => "1", "b" => "2");
echo $callback."(".json_encode($arr).")";
于 2013-05-21T05:40:33.490 回答