【发布时间】:2013-04-09 15:15:36
【问题描述】:
我有一个带有以下 ajax 方法的 html 页面:
$(document).ready(function() {
$.ajax({
type: "POST",
url: "http://www.webservice.com/blahblah.asmx/blahb123",
data: "tnWsGuid=TEST1",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(msg)
{
alert("sucess")
},
error: function(e)
{
alert(JSON.stringify(e));
}
});
});
这是返回给我的 403 禁止。一位同事构建了 Web 服务,我无法访问代码,也无法在他休假时更改它。我需要显示这些数据——此时我尝试的一切都失败了,并给了我一个 403 禁止错误。为了显示代码,我已将 url 名称和成功函数更改为警报对话框。
另一件事是 json 似乎被包装在 XML 中,来自一个看起来像这样的 ASP.NET 网络服务:
<string xmlns="http://Walkthrough/XmlWebServices/">
{"approverName":"","emailAddress":"","companyName":"ABC","address":{"streetAddress1":"12 BlahBlah","streetAddress2":"","state":"ON","zipCode":"","country":"SO","phoneNumber":""},"tabledata:"[{"vendorPart":"AAAAA","partDescription":"N/A","price":"0.00","quantity":"28"},{"vendorPart":"BBBBBBB","partDescription":"N/A","price":"0.00","quantity":"3"},{"vendorPart":"CCCCCC","partDescription":"N/A","price":"0.00","quantity":"25"}]}
</string>
这不是我原本期望接收 json 的意图,但是我现在对此无能为力,必须用 XML 处理它,然后尝试将内部格式化的 json 转换为 json 对象。
我也尝试过以下方法:
$(document).ready(function() {
$.ajax({
type: "GET",
url: "http://www.webservice.com/blahblah.asmx/blahb123",
data: "tnWsGuid=TEST1",
dataType: "xml",
contentType: "text/xml",
success: function(msg)
{
alert("sucess")
},
error: function(e)
{
alert(JSON.stringify(e));
}
});
});
最后,我将在这里为我尝试过的两个示例(json/xml)输出来自 FireFox 的 Firebug 响应
Json:http://i.imgur.com/zJy4BvD.jpg
XML:http://i.imgur.com/6qiGVwQ.jpg
非常感谢您的阅读!
【问题讨论】:
-
如果不访问该 Web 服务,调试 Web 服务以确定它拒绝您的请求的原因非常困难。
-
根据您提供的非常有限的信息,该服务似乎没有授予其他来源的 JS 访问它的权限。您无法让网站上的浏览器从服务中读取数据。移动(至少一部分)你的逻辑服务器端。
-
我正在尝试联系此人,如果我能够访问 Web 服务,我可以做些什么来授予此权限?
-
developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS(删除了维基百科链接,因为它没有提到预检)
标签: jquery xml ajax json web-services