无法从URL获取JSON数组

时间:2013-05-28 20:30:13

标签: javascript jquery json rest

我正在与Resteasy合作,我在网址中发布了一个资源:'localhost:8080 / ressources / agences'

当我在浏览器中测试时,我得到:

[{"agence":"Agence Kettani","ville":"Agadir","adresse":"Cartier Hassan II, Agadir","tel":"0528252828","fax":null,"idBanque":1,"id":1},
     {"agence":"Abbatoire","ville":"Agadir","adresse":"Abbatoire, 358 ","tel":"0528283569","fax":null,"idBanque":2,"id":2}]

但是当我尝试使用jQuery获取数组时,我得到了这个:

readyState
    0

responseText
    ""

status
    0

statusText
    "error"

abort
    function()

always
    function()

complete
    function()

done
    function()

error
    function()

fail
    function()

getAllResponseHeaders
    function()

getResponseHeader
    function()

isRejected
    function()

isResolved
    function()

overrideMimeType
    function()

pipe
    function()

promise
    function()

setRequestHeader
    function()

statusCode
    function()

success
    function()

then
    function()

这就是我在Firebug中所做的事情:

$.get('http://localhost:8080/Paie1Web/ressources/agences', function(data) {
  return alert(data);
});

我已经测试了getJSON和AJAX。我怎么能纠正这个?

1 个答案:

答案 0 :(得分:0)

有时成功时,由于延迟,服务器减速等原因,ajax有效负载尚未加载,因此您需要处理可以使用完整或已完成(取决于您使用的版本),可能是这样的:< / p>

var payload = {};
$.get('http://localhost:8080/Paie1Web/ressources/agences', function(data) {
   $.extend(
      true,      //this forces the object to be deep copied
      payload,   //the target object to extend
      data       //Extend to this object
   );
}, 'json').complete(function(){
   //do what ever you want here with the extended payload
});

我认为这样可行