【问题标题】:Network Error 405: Post made from Browser to Localhost WCF REST Web Service网络错误 405:从浏览器发布到 Localhost WCF REST Web 服务
【发布时间】:2013-10-13 07:19:25
【问题描述】:

我正在编写一个使用 dotnet rest web 服务的网站。我对这两个项目都有完全的控制权。

就在今天,我实现了一个 post web 服务。当我使用 Fiddler 发布时,它可以工作。今天早上我从网站上进行了一些测试,一切似乎都很好。我处理了另一个问题(使用 fiddler 正确获取帖子内容),然后我从浏览器返回发布,但现在整个网站都表现出 CORS 行为 - 空白返回等。此外,刚刚实现的帖子刚刚开始返回 405 方法不允许错误

"NetworkError: 405 Method Not Allowed - http://localhost:50122/MyService/ListMarkers"

这是网站代码:

 function postAjax(){
    var markers = 
    { "listmarkers":
    [{ "position": "128.3657142857143", "markerPosition": "7" },
                   { "position": "235.1944023323615", "markerPosition": "19" },
                   { "position": "42.5978231292517", "markerPosition": "-3" }]
    };

    $.ajax({
        type: "POST",
        url: window.url + "/PocShimService.svc/ListMarkers",
        // The key needs to match your method's input parameter (case-sensitive).
        data: JSON.stringify(markers),
        //data: markers,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data){alert(data);},
        failure: function(errMsg) {
            alert(errMsg);
        }
    });

}

这是与访问控制有关的服务器配置:

<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Content-Type" />
  </customHeaders>
</httpProtocol>

有什么想法吗?有什么建议吗?

【问题讨论】:

  • 发布您的服务代码...

标签: .net json wcf


【解决方案1】:

如果您的 WCF 和客户端应用的域名相同,那么您必须尝试使用​​ window.location.origin 而不是 window.url。你的 ajax 请求应该看起来像

$.ajax({
    type: "POST",
    url: window.location.origin + "/PocShimService.svc/ListMarkers",
    // The key needs to match your method's input parameter (case-sensitive).
    data: JSON.stringify(markers),
    //data: markers,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data){alert(data);},
    failure: function(errMsg) {
        alert(errMsg);
    }
});

【讨论】:

  • 这确实有效,我需要将网站移动到与 Web 服务相同的 url/端口。我想避免在与我的网络服务相同的地方运行网站。有什么建议吗?
  • 在这种情况下,您必须在服务器端调用 WCF,因为跨浏览器安全问题
猜你喜欢
  • 2015-10-24
  • 1970-01-01
  • 2017-03-25
  • 1970-01-01
  • 2021-03-23
  • 2018-05-13
  • 2013-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多