Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
400错误请求JQuery WCF服务_Jquery_Asp.net_Json_Wcf_Web Services - Fatal编程技术网

400错误请求JQuery WCF服务

400错误请求JQuery WCF服务,jquery,asp.net,json,wcf,web-services,Jquery,Asp.net,Json,Wcf,Web Services,我在VS2012的ASP.NET 4.5中创建了一个WCF web服务,该服务返回JSON响应。 该服务与内置的webservice客户端配合良好,我在Web.config文件中正确设置了端点 在客户端,我在Jquery中有一个简单的测试脚本,但是,在运行该脚本之后,我得到一个HTTP/1.1400错误请求 web服务根本不需要输入,因此data:被设置为空字符串 以下是调用该服务时得到的信息(使用FireFox中的HTTP Live Headers插件) 还有剧本: $(document).r

我在VS2012的ASP.NET 4.5中创建了一个WCF web服务,该服务返回JSON响应。 该服务与内置的webservice客户端配合良好,我在Web.config文件中正确设置了端点

在客户端,我在Jquery中有一个简单的测试脚本,但是,在运行该脚本之后,我得到一个HTTP/1.1400错误请求

web服务根本不需要输入,因此data:被设置为空字符串

以下是调用该服务时得到的信息(使用FireFox中的HTTP Live Headers插件)

还有剧本:

$(document).ready(function () {


    $('#btnRefresh').click(function () {

       $.ajax({
            type: 'GET',
            url: 'http://localhost:58234/CCSVC.svc/Get_BTCE_BTC_USD',
            data: '',
            contentType: 'application/json',
            dataType: 'json',
            processData: true,
            crossDomain: true,
            success: function (msg) {
                ServiceSucceeded(msg);
            },
            error: function (msg) {
                ServiceFailed(msg);
            }
        });


        function ServiceSucceeded(result) {
            alert("success");
        };

        function ServiceFailed(result) {
            alert("fail");
        };


    });




});
当然,它失败了。我尝试过几种不同的组合,但都没有运气和帖子,也没有机会。跨域真/假、processData真/假等。似乎什么都不起作用

这是Web服务的合同:

<OperationContract()>
<WebInvoke(BodyStyle:=WebMessageBodyStyle.Bare, RequestFormat:=WebMessageFormat.Json, ResponseFormat:=WebMessageFormat.Json)>
Function Get_BTCE_BTC_USD() As TradeData
更改webHttpBinding后出现新错误:

http://localhost:58234/CCSVC.svc/Get_BTCE_BTC_USD

OPTIONS /CCSVC.svc/Get_BTCE_BTC_USD HTTP/1.1
Host: localhost:58234
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0
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
DNT: 1
Origin: http://localhost:59099
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

HTTP/1.1 500 Internal Server Error
Cache-Control: private
Content-Length: 513
Content-Type: application/xml; charset=utf-8
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RTpcc2l0ZXMyXENyeXB0b0NvaW5TZXJ2aWNlc1xDQ1NWQy5zdmNcR2V0X0JUQ0VfQlRDX1VTRA==?=
X-Powered-By: ASP.NET
Date: Mon, 23 Dec 2013 17:02:44 GMT

由于您正在进行跨域通信,默认情况下WCF不支持跨域通信。请查看以下链接以获取完整示例


我创建了一个示例WCF,它与您解释的场景配合使用。然而,代码是用c#编写的。您可以尝试下面的配置和代码

  namespace WCF
{
    [ServiceContract(Namespace = "Testing")]
    //[ServiceContract]
    public interface ITestWCF
    {
        [OperationContract]
        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        String HelloWorld();

    }
}



namespace WCF
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService2" in both code and config file together.
    class TestWCF : ITestWCF
    {
        public string HelloWorld()
        {

            return "Hello!!!";
        }

    }
}
Ajax调用:

$.ajax({
            url: "Service1.svc/rest/HelloWorld?",
            type: "POST",
            data: "{}",       
            success: fnsuccesscallback,
            contentType: "application/json; charset=utf-8",
            error: fnerrorcallback,
            dataType: "json"
        });
Web.config:

<system.serviceModel>   
<client />
<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpEndpointBinding">
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="None" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<services>        
  <service name="WCF.TestWCF" behaviorConfiguration="TestWCFBehaviour">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:1991/Service1.svc"/>
      </baseAddresses>
    </host>
    <endpoint address="rest" binding="webHttpBinding" contract="WCF.ITestWCF" behaviorConfiguration="TestWCFEndPointBehaviour" name="httpEndpoint"></endpoint>
    <endpoint address="soap" binding="basicHttpBinding" contract="WCF.ITestWCF" bindingConfiguration="BasicHttpEndpointBinding"/>
    <endpoint address="mex"  binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="TestWCFBehaviour">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="TestWCFEndPointBehaviour">
      <!--<enableWebScript/>-->
      <webHttp/>
    </behavior>
  </endpointBehaviors>
</behaviors>


显示服务器端配置!如果您可以在WCF测试客户端中轻松地测试您的服务,那么它很可能是基于SOAP的服务-但是jQuery通常需要一个基于REST的服务(使用
webHttpBinding
)-您可以使用
basicHttpBinding
,一个基于SOAP的绑定-这非常有效,只是不能与jQuery结合使用。您需要研究
webHttpBinding
以及如何在服务中利用它,以便jQuery客户端可以调用您的服务方法。我添加了web.config内容。是的,正如您从合同装饰中看到的那样,它确实设置为JSON/REST。有更多的配置吗?好的-我将尝试webHttpBindingreplace数据:“”与数据:“{}”,好的-我将检查这一点。我不知道这与WCF有关:(极客,那是一个控制台应用程序吗?只是好奇。不,也不是这样-作者缺少太多代码来创建一个工作示例。然而,他确实在配置文件中提供了线索。另一天,又有一缕头发从头部拔出。唉!这是互联网上唯一有效的示例!!!我说得太快了。作者的示例只适用于lo凯莉(你不能像写的那样在网上分享)。接下来还有更多的实验。
  namespace WCF
{
    [ServiceContract(Namespace = "Testing")]
    //[ServiceContract]
    public interface ITestWCF
    {
        [OperationContract]
        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        String HelloWorld();

    }
}



namespace WCF
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService2" in both code and config file together.
    class TestWCF : ITestWCF
    {
        public string HelloWorld()
        {

            return "Hello!!!";
        }

    }
}
$.ajax({
            url: "Service1.svc/rest/HelloWorld?",
            type: "POST",
            data: "{}",       
            success: fnsuccesscallback,
            contentType: "application/json; charset=utf-8",
            error: fnerrorcallback,
            dataType: "json"
        });
<system.serviceModel>   
<client />
<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpEndpointBinding">
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="None" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<services>        
  <service name="WCF.TestWCF" behaviorConfiguration="TestWCFBehaviour">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:1991/Service1.svc"/>
      </baseAddresses>
    </host>
    <endpoint address="rest" binding="webHttpBinding" contract="WCF.ITestWCF" behaviorConfiguration="TestWCFEndPointBehaviour" name="httpEndpoint"></endpoint>
    <endpoint address="soap" binding="basicHttpBinding" contract="WCF.ITestWCF" bindingConfiguration="BasicHttpEndpointBinding"/>
    <endpoint address="mex"  binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="TestWCFBehaviour">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="TestWCFEndPointBehaviour">
      <!--<enableWebScript/>-->
      <webHttp/>
    </behavior>
  </endpointBehaviors>
</behaviors>