javascript - 无法从 URL 获取 JSON 数组

标签 javascript jquery json rest

我正在使用 Resteasy,我已经在 URL 中启动了一个资源:'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。我该如何纠正这个问题?

最佳答案

有时在成功时,由于延迟、服务器速度变慢等原因,ajax 有效负载尚未加载,因此您需要处理可以使用 complete 或 done(取决于您使用的版本),可能像这样:

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
});

我觉得可以

关于javascript - 无法从 URL 获取 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16800938/

相关文章:

javascript - 如何导入 400 多个模块,并在我的 angular-cli 项目中迭代它们?

Javascript 菜单下拉菜单在点击时不触发功能

javascript - React-Native:for 循环中的 this.something.map 可以访问 .json 文件中数据数组中的数据..有人可以建议一个示例代码吗?

Javascript:匹配变量名称并应用于匹配的变量

javascript - 通过 JXA 通过消息发送 SMS 时,克服错误 : Expecting object specifier. 参数没有对象说明符的更优雅的方法是什么

Javascript:如何将对象数组转换为具有排序的唯一数组的对象?

javascript - 根据选中的复选框数量生成动态 li 元素

javascript - Jquery 添加计算值

sql - 选择表作为 json 数组

java - 大数字反序列化抛出 NumberFormatException