【发布时间】:2017-11-28 08:09:48
【问题描述】:
我想请你帮忙解决我在 HTML 页面上调用 ASP.NET webservice 方法的问题。该代码非常简单,但通常无法正常工作。一般来说,因为我找到了一个浏览器 - IE 11,所以它运行良好。在所有其他浏览器上,Web 服务响应为空(XMLHttpRequest 对象的状态属性返回 0 而不是 200)。有趣的是,如果我直接将方法地址 (http://www.rpdp.hostingasp.pl/RPDPWebService.asmx/HelloWorld) 放到任何浏览器的 URL 栏中,结果都是正确的:
亲切的问候,
皮奥特雷克
附:我在外部托管 ASP.NET 网络服务器上发布我的网络服务,因此我无法更改它的(服务器)设置。
ASP.NET 网络服务:
namespace RPDP
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class RPDPWebService : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}
在 web.config.xml 文件中,我添加了以下标签(first in , second - in ):
<webServices>
<protocols>
<add name="HttpSoap"/>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Headers" value="accept, content-type" />
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="POST, GET, OPTIONS" />
</customHeaders>
</httpProtocol>
HTML 页面,我在该页面上调用我的 webservice 方法:
<html>
<head>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
function go()
{
var req = new XMLHttpRequest();
req.open("GET", "http://www.rpdp.hostingasp.pl/RPDPWebService.asmx/HelloWorld", true);
req.onreadystatechange = function(){
if ( req.readyState == 4) {
if ( req.status >= 200 && req.status < 300 ||req.status == 304 ) {
var returnData = req.responseText;
alert('Success! Returned data: ' + returnData);
} else {
alert("Error; request.status = " + req.status);
}
req = null;
};
}
req.send();
}
</script>
</head>
<body onload="go();"></body>
</html>
【问题讨论】:
标签: javascript asp.net json ajax asmx