【问题标题】:cannot get REST response using $.ajax()无法使用 $.ajax() 获得 REST 响应
【发布时间】:2014-06-06 09:51:15
【问题描述】:

下面是我的方法

function getCurrentUserName() 
{
    $.ajax({ 
    type: "GET",
    url: "http://localhost:8099/rest/prototype/1/space/ds",
    crossDomain:true,
    dataType: "jsonp",  
    success: function(resp){  
        alert("Server said123:\n '" + resp.name + "'");  
        },
    error:function(e){
    alert("Error"+e)
    }
    });
    });
}

我在单击按钮时调用它,我没有看到任何警报,当我在浏览器上键入 url 时,我得到了 xml 中的响应。但是我没有在脚本中得到响应。 有人可以帮我吗?我在这里错过了什么吗? 谢谢:)

【问题讨论】:

  • 如果确实提出了请求,您是否检查过开发者工具?
  • 添加错误函数并放置laert来检查是否有错误发生
  • 是的,请求显示在浏览器控制台中。它返回 [HTTP/1.1 200 OK 31ms]
  • 你能展示你的控制器方法来处理你的请求吗
  • 你遇到了什么错误?

标签: javascript jquery ajax rest


【解决方案1】:

看起来像 JSON 解析错误。

尝试使用json 而不是jsonp

function getCurrentUserName() 
{
    $.ajax({ 
    type: "GET",
    url: "http://localhost:8099/rest/prototype/1/space/ds",
    crossDomain:true,
    dataType: "json",  
    success: function(resp){  
        alert("Server said123:\n '" + resp.name + "'");  
        },
    error:function(e){
    alert("Error"+e)
    }
    });
    });
}

【讨论】:

  • 我得到同样的错误.. rest 调用返回的数据是 xml 格式。我需要它在 Json 中。我也在尝试下面的代码 '$.ajax({ type: 'GET', url: "localhost:8099/rest/prototype/1/space/ds", data: {}, dataType: "xml", success: function (xml){ var clientid = $( xml).find('avatarUrl').first().text(); alert(""+clientid); } });'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-21
  • 1970-01-01
  • 2017-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多