1

Localhost からサーバーへの Jquery を使用して WCF サービスを使用しようとしていますが、「405 Method Not Allowed」というメッセージが表示され続けます。

これは私が使用している構成です:

<system.serviceModel>
<bindings>
  <webHttpBinding>
      <binding name="jsonp" crossDomainScriptAccessEnabled="true" />
      <binding name="jsonpSsl" crossDomainScriptAccessEnabled="true">
          <security mode="Transport" />
      </binding>
  </webHttpBinding>
</bindings>
<behaviors>
    <serviceBehaviors>
       <behavior name="MyBehavior">
           <serviceMetadata httpGetEnabled="true" />
           <serviceDebug includeExceptionDetailInFaults="true" />
      </behavior>
   </serviceBehaviors>
   <endpointBehaviors>
       <behavior name="MyBehavior">
          <webHttp />        
       </behavior>
    </endpointBehaviors>
</behaviors>
<services>
 <service behaviorConfiguration="MyBehavior" 
              name="MyWs.Service1">
            <endpoint address="" binding="webHttpBinding"
               bindingConfiguration="jsonp" contract="MyWs.IService1" 
               behaviorConfiguration="MyBehavior"/> 
            <endpoint address="" binding="webHttpBinding"
               bindingConfiguration="jsonpSsl" contract="MyWs.IService1" 
               behaviorConfiguration="MyBehavior"/>                        
     </service>
</services>

JQuery 関数:

   $.ajax({
            type: "POST",
            url: "http://XXX.XXX.XXX.XXX/wcf/Service1.svc/GetData",
            data: '{"value":"2340"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccessCall,
            error: OnErrorCall
        });

        function OnSuccessCall(response) {
            alert(response);
        }


        function OnErrorCall(response) {
            alert(response.status + " " + response.statusText);
        }

WCF インターフェイス:

  [OperationContract]
    [WebInvoke(Method = "POST",
    BodyStyle = WebMessageBodyStyle.Wrapped,
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json)]
    string GetData(int value);

メソッドの実装:

 public string GetData(int value)
    {
        WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
        System.Web.Script.Serialization.JavaScriptSerializer oSerializer =
         new System.Web.Script.Serialization.JavaScriptSerializer();
        string json = oSerializer.Serialize(string.Format("You entered: {0}", value));
        return json;
    }

それを解決する方法はありますか?

ありがとう!

4

2 に答える 2

0

この手順に従ってください。http://www.codeproject.com/Articles/223572/Calling-Cross-Domain-WCF-service-using-Jquery-Java

于 2012-06-27T14:00:21.510 に答える
0
[OperationContract]
[WebGet( ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, RequestFormat=WebMessageFormat.Json,
        UriTemplate = "GetEmployeeJson")]
  List<EmployeeData> GetEmployeeJson();

web.config

  <bindings>
      <webHttpBinding>
          <binding name="webHttpBindingWithJsonP"
                   crossDomainScriptAccessEnabled="true" />
      </webHttpBinding>
  </bindings>
  <behaviors>
      <serviceBehaviors>
          <behavior name="WcfExample.Service1Behavior">
              <serviceMetadata httpGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="true"/>
          </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
          <behavior name="WebBehavior">
              <webHttp/>
          </behavior>
      </endpointBehaviors>
  </behaviors>
  <services>
      <service behaviorConfiguration="WcfExample.Service1Behavior" name="WcfExample.Service1">
          <endpoint address="" binding="webHttpBinding" contract="WcfExample.IService1" bindingConfiguration="webHttpBindingWithJsonP" behaviorConfiguration="WebBehavior" />
      </service>
  </services>

wcfサービスへのjquery呼び出し

       $.ajax({
            type: "GET",
            contentType: "application/javascript",
            crossDomain: true,
            dataType: 'jsonp',
            cache: true,
            url: 'http://localhost:49349/Service1.svc/GetEmployeeJson',
            success: function (data) {
                var html = [];

                alert(data[0].lastname);


                $.each(data, function (index, value) {
                    $("#TableID").append("<tr><td>" + value.HREmpId + "</td><td>" + value.firstName + "</td><td>" + value.lastname + "</td><td>" + value.address + "</td><td>" + value.city + "</td></tr>");

                });


            },

            error: function (xhr, ajaxOptions, thrownError) {
                alert("here error");
                alert(thrownError);
                if (xhr != null) {

                    var err = JSON.parse(xhr.responseText); //you can throw a code-behinde Exception and it will automatically                                                 //render to a valid JSON string when we rerieve the responseText
                    alert("ErrorMessage: " + err.Message + " StackTrace: " + err.StackTrace);

                }
            }
        });
于 2015-03-18T07:39:24.817 に答える