2011-04-11 22 views
5

ajax'den bir WCF hizmetini arıyorum ve bir GET isteği olarak çalışmasını ancak bir POST isteği olarak çalışmasını sağlayabiliyorum. Yani:ajax POST çağrısı WCF hizmetine sorun

[OperationContract] 
    [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
    public UserObject GetUser(string name) 
    { 
     // Add your operation implementation here 
     var uo = new UserObject() { CustClass = "Guest", fname = "Chris", Email = "[email protected]", IsMobile = false }; 
     uo.fname = name; 
     return uo; 
    } 

ve

var json = { "name": "test" }; 
$.ajax({ //get user name and customer class 
    type: "GET", 
    url: "WritingAnalysisService.svc/GetUser", 
    data: json, 
    processData: true, 
    contentType: "application/json", 
    timeout: 10000, 
    dataType: "json", 
    cache: false, 
    success: function (data) { //get user name and customer class 
     customerclass = data.d.custclass; 
     ffname = data.d.fname; 
    } 
}); 

çalışır ancak:

[OperationContract] 
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
    public UserObject GetUser(string name) 
    { 
     // Add your operation implementation here 
     var uo = new UserObject() { CustClass = "Guest", fname = "Chris", Email = "[email protected]", IsMobile = false }; 
     uo.fname = name; 
     return uo; 
    } 

ve

$.ajax({ //get user name and customer class 
    type: "POST", 
    url: "WritingAnalysisService.svc/GetUser", 
    data: json, 
    processData: true, 
    contentType: "application/json", 
    timeout: 10000, 
    dataType: "json", 
    cache: false, 
    success: function (data) { //get user name and customer class 
     customerclass = data.d.custclass; 
     ffname = data.d.fname; 
    } 
}); 

yapmaz. Basit bir şey mi eksik? Saçlarımı yırtıyorum. Teşekkürler

+0

'Yapmıyor. Bu ne demek? Hata mı alıyorsun? Servis hiç çağrılmıyor mu? java komut hatası aldın mı? Fiddler'da istek ve cevap nedir? – Aliostad

cevap

1

Benzer örnek projem vardı. Ben burada yazılan, o yardımcı olur umarım:

Web.config:

<system.serviceModel> 
    <behaviors> 
     <endpointBehaviors> 
      <behavior name="WebFormApp.MyWcfServiceAspNetAjaxBehavior"> 
       <enableWebScript/> 
      </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> 
    <services> 
     <service name="WebFormApp.MyWcfService"> 
      <endpoint address="" behaviorConfiguration="WebFormApp.MyWcfServiceAspNetAjaxBehavior" binding="webHttpBinding" contract="WebFormApp.MyWcfService"/> 
     </service> 
    </services> 
</system.serviceModel> 

WCF:

<ServiceContract(Namespace:="")> _ 
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> _ 
Public Class MyWcfService 

    <OperationContract(), _ 
    WebInvoke(Method:="POST", _ 
       BodyStyle:=WebMessageBodyStyle.WrappedRequest, _ 
       ResponseFormat:=WebMessageFormat.Json)> _ 
    Public Function GetTheEntity() As MyEntity 
     Return New MyEntity With {.Name = "TheName", .Family = "TheFamily"} 
    End Function 

End Class 

JS:

function doTestJQuery() { 
       $.ajax({ 
        type: "POST", 
        url: "MyWcfService.svc/GetTheEntity", 
        data: null, 
        contentType: "application/json; charset=utf-8", 
        dataType: "json", 
        processdata: true, 
        success: srvSuccessJQuery, 
        error: null 
       }); 
      } 

      function srvSuccessJQuery(result) { 
       alert(result.d.Family) 
      }; 
2

Kullanım

BodyStyle = WebMessageBodyStyle.WrappedRequest 
WebInvokeAttribute için

+0

Teşekkürler. Unuttuğum basit şey – Firnas