简体   繁体   English

400尝试使用XHR联系WCF服务时出现错误请求

[英]400 Bad request when trying to contact WCF service using XHR

I'm making a WCF web service that will return a json object, but I keep getting a 400 bad request error when I try to make an AJAX call: 我正在创建一个将返回json对象的WCF Web服务,但是当我尝试进行AJAX调用时,我仍然遇到400错误请求错误:

OPTIONS http://localhost:55658/WebServiceWrapper.svc/GetData?_=1318567254842&value=97            HTTP/1.1
Host: localhost:55658
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
DNT: 1
Connection: keep-alive
Origin: http://localhost:3000
Access-Control-Request-Method: GET
Access-Control-Request-Headers: content-type

HTTP/1.1 400 Bad Request
Server: ASP.NET Development Server/10.0.0.0
Date: Fri, 14 Oct 2011 04:40:55 GMT
X-AspNet-Version: 4.0.30319
Cache-Control: private
Content-Length: 0
Connection: Close

Here is my AJAX call: 这是我的AJAX调用:

$.ajax({
  contentType: 'application/json',
  url: 'http://localhost:55658/WebServiceWrapper.svc/GetData',
  dataType: 'json',
  data: {
    value: 97
  },
  success: function (data) {
    alert('success' + data);
  },
  error: function(XMLHttpRequest, textStatus, errorThrown) {
    alert('failure' + errorThrown);
  }
});

Here is my WCF service definition: 这是我的WCF服务定义:

public class WebServiceWrapper : IWebServiceWrapper
{
    public object GetData(int value)
    {
        return new 
        {
            ReturnValue = string.Format("You entered: {0}", value)
        };
    }
}

And it's interface: 它的界面:

[ServiceContract]
public interface IWebServiceWrapper
{
    [OperationContract]
    object GetData(int value);
}

I know I've solved this problem before, but I can't remember what I had done before. 我知道我之前已经解决了这个问题,但我不记得我以前做过什么了。 Any help would be greatly appreciated as the hole I'm putting in the wall is just getting bigger and bigger. 任何帮助都会非常感激,因为我放在墙上的洞越来越大了。

There a couple of things you may need to do (or check if you have done): 您可能需要做一些事情(或检查您是否已经完成):

Your GetData OperationContract needs to be decorated with [WebGet] attribute. 您的GetData OperationContract需要使用[WebGet]属性进行修饰。


    [WebGet]
    [OperationContract]
    object GetData(int value);

Your WebServiceWrapper class needs to be decorated with [AspNetCompatibilityRequirements] attribute (Add a reference to System.ServiceModel.Web for this to be available) 您的WebServiceWrapper类需要使用[AspNetCompatibilityRequirements]属性进行修饰(添加对System.ServiceModel.Web的引用以使其可用)


    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class WebServiceWrapper : IWebServiceWrapper

The return value from GetData operation will need to be wrapped. 需要包装GetData操作的返回值。 An easy way is to use a JavaScriptSerializer to serialize the object to json 一种简单的方法是使用JavaScriptSerializer将对象序列化为json


    var serializer = new JavaScriptSerializer();
    return serializer.Serialize(new
                                {
                                    ReturnValue = string.Format("You entered: {0}", value)
                                });

You need to make sure you are using a [webHttpBinding] in your endpoints (client and services) 您需要确保在端点(客户端和服务)中使用[webHttpBinding]

<endpoint ... binding="webHttpBinding" ... />

I dont know if this next thing is mandatory, but you may need an endpoint behaviour that enables web script 我不知道下一件事是否必须,但您可能需要一个启用Web脚本的端点行为

<endpointBehaviors>
    <behavior name="[endpointBehaviorName]">
        <enableWebScript/>
    </behavior>
</endpointBehaviors>

You will obviously need to refer to this behavior configuration in your endpoint(s) 您显然需要在端点中引用此行为配置

<endpoint ... behaviorConfiguration="[endpointBehaviorName]" ... />

Looking at your request you are making an OPTIONS request rather than a GET, which I think jQuery does automatically if you try to do a cross-site XHR request, see this question: 查看您的请求,您正在发出OPTIONS请求而不是GET,我认为如果您尝试执行跨站点XHR请求,jQuery会自动执行,请参阅此问题:

Why am I getting an OPTIONS request instead of a GET request? 为什么我收到OPTIONS请求而不是GET请求?

I'm not even sure that your server supports the OPTIONS method, hence your 400 error. 我甚至不确定您的服务器是否支持OPTIONS方法,因此您的400错误。

Even different port numbers are considered cross-site requests, so you'd better make a proxy page on the actual web site that makes the HTTP request to your WCF Service. 即使不同的端口号也被视为跨站点请求,因此您最好在实际网站上创建一个代理页面,该页面向您的WCF服务发出HTTP请求。

You seem to be violating the same origin policy restriction . 您似乎违反了相同的原始政策限制 You cannot send cross domain AJAX requests. 您无法发送跨域AJAX请求。 Your application is hosted on http://localhost:3000 and you are trying to send an AJAX request to http://localhost:55658 . 您的应用程序托管在http://localhost:3000并且您正在尝试向http://localhost:55658发送AJAX请求。 That's impossible. 这不可能。 You can send AJAX requests only to http://localhost:3000 . 您只能将AJAX请求发送到http://localhost:3000 You could use JSONP to circumvent this restriction. 您可以使用JSONP来规避此限制。 And here's another article on MSDN. 这是MSDN上的另一篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM