我从2天开始就遇到了这个问题,我需要使用javascript从以下网址获取数据。
http://www.apexweb.co.in/Apex_Quote/Download_Data.asp?un=9393910169
我解析了静态数据,但我需要解析网址中的数据。帮帮我
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>
答案 0 :(得分: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");
}
});
从您的网址中提取的数据将作为data
回调中的success
参数传递。反对$.getJson,这使我能够更好地控制我的回归。
在这种方法中,我将dataType设置为text
。意思是jquery不会尝试用我的结果做任何“聪明”的事情。如果我将其设置为json
,则data
参数将被解析并准备使用。您不需要手动解析它:
var employees = $.parseJSON(data);
答案 1 :(得分: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).")";