0

嗨,你能帮我吗?我有许多网页,我需要知道它们被加载了多少次。我创建了一个 Web 服务,用于在触发时将记录输入数据库。

<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

该网络服务运行良好。

然后,我在将调用 Web 服务的同一个 VS2012 项目中创建了一个测试 HTML。

    <!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>

有人能帮我从不同域的网页调用网络服务吗?

谢谢保罗

4

0 回答 0