jQuery .ajax()POST请求在RESTful WCF上抛出405(不允许使用方法) [英] jQuery .ajax() POST Request throws 405 (Method Not Allowed) on RESTful WCF

查看:198
本文介绍了jQuery .ajax()POST请求在RESTful WCF上抛出405(不允许使用方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向RESTFUL WCF服务应用程序发送发布请求.我能够通过Fiddler成功发送POST请求.

I am sending a post request to a RESTFUL WCF service application. I am able to successfully send a POST request through Fiddler.

但是,当我通过jQuery Ajax方法执行此操作时,该函数会将以下内容返回到Chrome开发者控制台:

However when I do this through the jQuery Ajax method the function returns the following to the Chrome Developer Console:

OPTIONS http://www.example.com/testservice/service1.svc/GetData 405 (Method Not Allowed) jquery.min.js:6

但是在日志之后一秒钟:

But then a second after logs:

Object {d: "You entered 10"} testpost.html:16

这告诉我jQuery正在发送 OPTIONS 请求,该请求失败,然后发送POST请求,该请求返回期望的数据.

What this tells me is that jQuery is sending a OPTIONS request, which fails, and then sending a POST request which returns the expected data.

我的jQuery代码:

My jQuery Code:

$.ajax() {        
type: "POST", //GET or POST or PUT or DELETE verb 
    url: "http://www.example.com/testservice/service1.svc/GetData", // Location of the service      
    data: '{"value":"10"}', //Data sent to server
    contentType:"application/json",
    dataType: "json", //Expected data format from server    
    processdata: false,
    success: function (msg) {//On Successfull service call   
        console.log(msg);
    },
    error: function (xhr) { console.log(xhr.responseText); } // When Service call fails             
});

我正在使用jQuery 2.0.2版.

I am using jQuery version 2.0.2.

任何有关为什么会发生此错误的帮助都将是一个很大的帮助.

Any help on why this error is occurring would be a great help.

推荐答案

您的代码实际上是在尝试创建跨域(CORS)请求,而不是普通的POST.

也就是说,现代浏览器将只允许对与HTML页面相同域中的服务进行Ajax调用.

That is: Modern browsers will only allow Ajax calls to services in the same domain as the HTML page.

示例:http://www.example.com/myPage.html中的页面只能直接请求http://www.example.com中的服务,例如http://www.example.com/testservice/etc.如果该服务在其他域中,则浏览器将不会进行直接调用(如您期望的那样).相反,它将尝试发出CORS请求.

Example: A page in http://www.example.com/myPage.html can only directly request services that are in http://www.example.com, like http://www.example.com/testservice/etc. If the service is in other domain, the browser won't make the direct call (as you'd expect). Instead, it will try to make a CORS request.

简而言之,要执行CORS请求,请使用您的浏览器:

To put it shortly, to perform a CORS request, your browser:

  • 将首先向目标网址发送OPTION请求
  • 然后(仅在服务器对该OPTION的响应中包含足够的标头(Access-Control-Allow-Origin是其中之一)以允许CORS请求,浏览器将执行调用(几乎完全相同,如果HTML页面位于同一域中).
    • 如果期望的标头没有出现,浏览器只会放弃(就像您所做的那样).
    • Will first send an OPTION request to the target URL
    • And then only if the server response to that OPTION contains the adequate headers (Access-Control-Allow-Origin is one of them) to allow the CORS request, the browse will perform the call (almost exactly the way it would if the HTML page was at the same domain).
      • If the expected headers don't come, the browser simply gives up (like it did to you).

      如何解决?最简单的方法是在服务器上启用CORS(启用必要的标头).

      How to solve it? The simplest way is to enable CORS (enable the necessary headers) on the server.

      如果您无权对其进行服务器端访问,则可以从其他地方镜像该Web服务,然后在该处启用CORS.

      If you don't have server-side access to it, you can mirror the web service from somewhere else, and then enable CORS there.

      这篇关于jQuery .ajax()POST请求在RESTful WCF上抛出405(不允许使用方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆