如何jQuery JSONP从URL中检索数据?

时间:2012-08-21 08:57:24

标签: jquery jsonp

我收到服务网址的回复,但正在寻找jQuery将数据放到网页上。有人可以用jQuery代码循环来帮助我吗?

谢谢,

<script type="text/javascript"> 


$.jsonp({
    url: "http://test.com",
    callback: "_jqjsp",
    success: function(json) {
       // Will be given the response of service

        alert("success");
       console.log("success");
    },
    error: function() {
       // Will be notified of an error requesting service
       alert("error");
       console.log("error");
    }
});

</script>

这是我的JSONP数据

_jqjsp({
"response":{
  "numFound":2,
  "docs":[
     {
        "id":"1",
        "Name":"22689",
        "Company":"ABC"

     },
     {
         "id":"2",
        "Name":"00012",
        "Company":"ABC
     },

  ]
}
});

1 个答案:

答案 0 :(得分:0)

我认为希望它可以帮助其他人:)

    $.jsonp({
json: "http://www.test.com/....",
callback: "_jqjsp",

success: function(json) { // json object

//Success
     $.each(json.response.docs, function(i,item) { // json is the name of the object

          $("#records").append('Test: </br>'); //item is named at the opening of this $.each function
          $("#records").append(item.abc + '</br>'); 
          $("#records").append(item.def + '</br>'); 
          $("#records").append('<p>'); 
          });     

            console.log(json);
            console.log("JSONP was retrieved successfully");
},
error: function() {
   // Will be notified of an error requesting service
   alert("error");
   console.log("error"); // This is for debugging
} });