从JQuery Ajax使用wcf服务应用程序

从JQuery Ajax使用wcf服务应用程序,wcf,jquery,get,http-status-code-405,Wcf,Jquery,Get,Http Status Code 405,我已经构建了一个WCF web应用程序,将其方法公开到get enabled方法中 [OperationContract] [WebGet] string getStatistics(); [OperationContract] [WebGet] string getVenues(string BrandName, int limit); 并编辑了配置文件: <endpoint address="json" binding="webHttpBinding" contract="fo

我已经构建了一个WCF web应用程序,将其方法公开到get enabled方法中

[OperationContract]
[WebGet]
string getStatistics();


[OperationContract]
[WebGet]
string getVenues(string BrandName, int limit);
并编辑了配置文件:

<endpoint address="json" binding="webHttpBinding"  contract="foursquare2RDF.IVenue2rdf" behaviorConfiguration="restBehavior"/>
它返回了一个良好的结果

问题是,如果显示以下错误,我无法使用此restful服务:

OPTIONS http://localhost:83/venue2rdf.svc/json/getStatistics?{'venues':'100'} 405 (Method Not Allowed) 

XMLHttpRequest cannot load [http://localhost:83/venue2rdf.svc/json/getStatistics][1]. Origin null is not allowed by Access-Control-Allow-Origin.
我正在使用该代码调用服务:

$.ajax({
    type: "get",
    url: statisticsURL,
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
        eval("var x = " + msg.d);

        console.log(x);
    }
});
到目前为止,我所达到的目标是:

  • 我尝试用类似问题中所述的$.getjson替换$.ajax 错误405被删除,第二个错误就出现了
  • 我发现了一个叫做支持Ajax的WCF服务项目,但我仍然不想迁移到一个新项目中
  • 我知道有类似的问题,但都不适合,显示出不同的错误,我的

您可能应该提出请求,因为您跨域访问时遇到了以下问题:

回调=?部分调用jquery使其成为JSONP。我建议您仔细阅读JSONP是什么,因为它不是一个银弹。要在WCF服务上启用JSONP,请阅读:


要使用jQuery使用跨域WCF REST服务,请查看以下示例:

我的服务如下:

    [ServiceContract]
    public interface IJSONPService
    {
        [OperationContract]
        [WebGet]
        string GetDate();

        [OperationContract]
        [WebInvoke]
        string PostData(string name);
    }
GET http://localhost/Service/JSONPService.svc/GetDate?callback=MyCallback&_=1343391683779 HTTP/1.1
Host: localhost
Connection: keep-alive
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/x-javascript
Date: Fri, 27 Jul 2012 12:21:23 GMT
Content-Length: 27

MyCallback("27\/07\/2012");
现在,上述服务的我的配置条目如下所示:

<services>
    <service name="Service.JSONPService">
        <endpoint address="" binding="webHttpBinding" behaviorConfiguration="json" bindingConfiguration="defaultRestJsonp" contract="Service.IJSONPService">
        </endpoint>
    </service>
</services>
<behaviors>
      <endpointBehaviors>
         <behavior name="json">
             <enableWebScript />
         </behavior>
   </behaviors>
</endpointBehaviors>
<webHttpBinding>
        <binding name="defaultRestJsonp" crossDomainScriptAccessEnabled="true">
          <readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="64" maxNameTableCharCount="2147483647" />
          <security mode="None" />
        </binding>
</webHttpBinding>
function TestingWCFRestWithJsonp() {
                $.ajax({
                    url: "http://domain.com/Service/JSONPService.svc/GetDate",
                    dataType: "jsonp",
                    type: "GET",
                    timeout: 10000,
                    jsonpCallback: "MyCallback",
                    success: function (data, textStatus, jqXHR) {
                        alert(data);
                    },
                    error: function (jqXHR, textStatus, errorThrown) {alert('error');

                    },
                    complete: function (jqXHR, textStatus) {alert('complete');
                    }
                });
            }
            function MyCallback(data) {
                alert(data);
            }
检查$.ajax方法调用中的jsonpCallback属性

对web服务调用的原始请求如下所示:

    [ServiceContract]
    public interface IJSONPService
    {
        [OperationContract]
        [WebGet]
        string GetDate();

        [OperationContract]
        [WebInvoke]
        string PostData(string name);
    }
GET http://localhost/Service/JSONPService.svc/GetDate?callback=MyCallback&_=1343391683779 HTTP/1.1
Host: localhost
Connection: keep-alive
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/x-javascript
Date: Fri, 27 Jul 2012 12:21:23 GMT
Content-Length: 27

MyCallback("27\/07\/2012");
来自WCF REST服务的原始响应如下所示:

    [ServiceContract]
    public interface IJSONPService
    {
        [OperationContract]
        [WebGet]
        string GetDate();

        [OperationContract]
        [WebInvoke]
        string PostData(string name);
    }
GET http://localhost/Service/JSONPService.svc/GetDate?callback=MyCallback&_=1343391683779 HTTP/1.1
Host: localhost
Connection: keep-alive
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/x-javascript
Date: Fri, 27 Jul 2012 12:21:23 GMT
Content-Length: 27

MyCallback("27\/07\/2012");

注意:当执行JSONP请求时,不会调用$.ajax方法error/complete/success。

好的,这确实有效,只是我想Jquery在解析结果时有问题。它返回非预期的令牌,我应该修改服务还是其他东西来接受jsonp?我猜wcf 4.0本机支持jsonpIf,如果您正在调用的项目与您的wcf服务位于同一个域中,则可以从JQuery调用。如果域不同,那么您需要像@the_ajp所说的那样使用JSONP,并且您需要确保您的WCF通过将结果写回响应流来处理JSONP请求。