JQuery async xml GET 请求 WCF Restful 服务失败
我有一个 JQuery ajax 函数正在调用我的 WCF Rest Web 服务。我的 Web 服务调用另一个返回 JSON 的服务。我不想创建所有 C# 类并解析 JSON 响应并返回它。因此,我只是创建了一个类,其中包含一个字符串属性,并将 json 作为字符串插入到该属性中,并将其作为 xml 返回。
在客户端,当我直接调用天气服务并返回 JSON 并享受 JQuery 的自动解析时,实际上客户端工作得非常好。现在我将数据类型更改为“xml”,它总是进入错误函数,没有有用的错误消息。
function getWeather(location) {
var requestURL = 'http://localhost:55120/Weather1.svc/Weather/' + location;
return $.ajax({
url: requestURL,
type: 'GET',
dataType: 'xml',
error: function (xhr, status, errorThrown) {
hideWeatherDiv();
var jsonVal = $.parseJSON(xhr.responseText);
$("#errorDiv").html('Response code: ' + xhr.status + '<br/>Response text: ' + jsonVal + '<br/>Status: ' +status + '<br/>Error thrown: ' + errorThrown);
}
});
}
var promise = getWeather(location);
promise.success(function (data) {
alert('promise success');
renderWeather(data);
return;
});
以上是该函数和我对它的调用。就像我提到的,它正在向我的 WCF 服务发出请求,该服务正在调用天气服务。我可以在 fiddler 中看到我的 WCF 请求的响应,它是有效的 XML,但我无法在这里解析它。
I have a JQuery ajax function that is calling a my WCF Rest web service. My web service calls another service which returns JSON. I did not want to create all the C# classes and parse that JSON response and return it. So I just created a class with one string property in it and jammed the json as a string in that property and returning it as xml.
On the client side, I actually had the client working absolutely fine when I directly was calling the weather service and getting back JSON and enjoyed automatic parsing by JQuery. Now I changed the dataType to 'xml' and it is always going into the error function with no useful error message.
function getWeather(location) {
var requestURL = 'http://localhost:55120/Weather1.svc/Weather/' + location;
return $.ajax({
url: requestURL,
type: 'GET',
dataType: 'xml',
error: function (xhr, status, errorThrown) {
hideWeatherDiv();
var jsonVal = $.parseJSON(xhr.responseText);
$("#errorDiv").html('Response code: ' + xhr.status + '<br/>Response text: ' + jsonVal + '<br/>Status: ' +status + '<br/>Error thrown: ' + errorThrown);
}
});
}
var promise = getWeather(location);
promise.success(function (data) {
alert('promise success');
renderWeather(data);
return;
});
Above are the function and my call to it. Like I mentioned, it is making request to my WCF service, which is calling the weather service. I can see the response in fiddler for my WCF request that it is valid XML, but I am unable to parse it here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论