【发布时间】:2014-04-08 17:51:42
【问题描述】:
我已经阅读了很多关于 ajax 问题的帖子,但我仍然不知道是什么导致了我的问题:
我正在尝试通过 ajax 调用 RESTful 服务(内部)以获取大型 json-String。
编辑:这就是我现在解决问题的方法 - 我知道这不是一个很好的解决方案,但由于我现在无法更改 web 服务上的任何内容,这就是我使用的:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
jQuery(document).ready(function(){
jQuery.support.cors = true;
jQuery.ajax({
url: "<internalPage>",
dataType: 'json'
})
.error(function(xhr, err, status) {
console.log(xhr + err + status);
})
.then(function(data) {
alert("success for me");
$.each(data.portalServices, function(index, value) {
$("#someId").append('<li>' + value.name + '</li>');
});
});
});
</script>
</head>
<body>
<ul id="someId"></ul>
</body>
</html>
编辑:这仅适用于 IE,而不适用于 FF。
警报是这样说的: 阿贾克斯错误 类型:获取 请求页面: 状态:0 - 错误 抛出的错误:
但在 Firebug 中:
Response-Header
Content-Type application/json
Date Thu, 06 Mar 2014 07:44:32 GMT
Server Apache-Coyote/1.1
Transfer-Encoding chunked
Anfrage-HeaderQuelltext anzeigen
Accept application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
Cache-Control no-cache
Connection keep-alive
Host xxxxxx
Origin null
Pragma no-cache
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0
Responce-Header from Cache
Content-Type application/json
Date Thu, 06 Mar 2014 07:44:32 GMT
Server Apache-Coyote/1.1
Transfer-Encoding chunked
状态为 200 正常 响应是一个有效的 Json(真的,我已经在线验证过) {"portalServices":[{"articleID":"ABR_1043024010","name":"SK04 托管服务器:Citrix Basisservice","portalname":"zentrale Arbeitsplatz-Umgebung (Citrix-Basis)","preis":18.0000, “beschreibung”:“Mit diesem Produkt stellen wir Ihnen eine zentrale Arbeitsplatz-Umgebung (auf der Basis von Citrix) bereit, die folgende Komponenten ....
但是没有 JSON 选项卡。
当我使用缓存 false 时,Firebug 没有显示响应,并且警报中显示的错误如下:
Ajax Error
Type:GET
Requesting Page: <internalPage>
Status: 0 - Exception ..."Failure" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location "JS frame :: <link to jquery> :: .send :: line 6 " data: no .....
我还尝试了对另一个成功的服务 ("http://rest-service.guides.spring.io/greeting") 的 ajax 调用。在firebug中有一个JSON选项卡,但这可能是因为从这个站点返回的json不是一个对象列表,比如从我的内部站点webservice返回的json。
有什么想法吗?我认为网络服务可能存在问题,但如果您能找到一些线索可能是什么原因,那就太好了。 非常感谢。
即使使用以下方法仍然无法正常工作=/
headers: {'Access-Control-Allow-Origin': '<internalPage>'},
dataType: 'jsonp',
crossDomain: true
internalPage 不在同一个域中,但不会响应来自外部网络的请求。在没有明确允许 crossDomain 或像上面那样设置标头的情况下,对 spring 服务的调用确实有效。
我会按照parsererror after jQuery.ajax request with jsonp content type 的建议尝试并回来告诉它是否有效。
编辑:WebServices 将在未来进行更改,以便我可以使用 jsonp 而不是强制 jQuery 允许跨域 Ajax-Calls。
编辑:WebService 已更改,因此现在可以正常使用数据类型 jsonp。 =)
感谢您的帮助。
【问题讨论】:
-
只需将 AJAX 调用中的 url 粘贴到浏览器中。你看到了什么?
-
@ Sandeep :我得到一个状态 500,但我想那是因为服务器期望数据类型为 json 而没有别的。当我尝试使用 restclient 调用服务时,我还必须将类型设置为 json 以获取响应。
-
状态 500 - 内部服务器错误。我认为服务器没有按要求发回 JSON。
dataType:'json'告诉服务器客户端期望 JSON 作为对 REST 调用的响应。 -
好吧,Firebug 确实在选项卡响应中显示了 json 字符串,并且 firefox 插件 restclient 也在接收 json
标签: jquery ajax json web-services