<WebMethod(Description:="Counts when a page is loaded")> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)>
Public Sub CountOnPageLoad(ByVal websiteName As String, websitePage As String)
Dim wp As New WebpageMonitoringRepository
wp.countPageLoad(websiteName, websitePage)
End Sub
此网络服务正常。
然后我在同一个VS2012项目中创建了一个测试HTML,它将调用webservice。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="Scripts/jquery-1.9.1.js"></script>
<script>
var data = "{'websiteName':'Test','websitePage':'Test'}"
$.ajax({
type: "POST",
url: "http://webservicesuite/WebpageMonitoringService.asmx/CountOnPageLoad",
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) { },
error: function (result) { }
});
</script>
</head>
<body>
</body>
</html>
这也可以正常工作并填充数据库。
然而,当我把它放在另一台服务器上的网页上时,我得到了Access-Control-Allow-Origin和Access-Control-Allow-Headers错误。我已经搞砸了几天了
我已经把
<httpProtocol>
<customHeaders>
<!-- Adding the following custom HttpHeader will help prevent CORS from stopping the Request-->
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
和
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
进入我的web.config文件。我用jsonp尝试了它,但仍然无法让它工作。
我现在已将jquery更改为javascript
<script language="text/javascript">
xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST","http://webservicesuite.*********.uk/WebpageMonitoringService.asmx/CountOnPageLoad",true);
xmlhttp.send("websiteName=Henry&websitePage=Ford");
</script>
请有人帮助我从其他域名的网页上拨打网络服务。
感谢保罗