2012-07-13 14 views
8

मैं बहुत की तरह एक ASPX पेज से एक WCF वेब सेवा को कॉल करने का प्रयास कर रहा हूं, उनसे WCF सेवा कॉलिंग:

var payload = { 
    applicationKey: 40868578 
}; 

$.ajax({ 
    url: "/Services/AjaxSupportService.svc/ReNotify", 
    type: "POST", 
    data: JSON.stringify(payload), 
    contentType: "application/json", 
    dataType: "json" 
}); 

ऐसा करने से वेब सर्वर में परिणाम त्रुटि 415 Unsupported Media Type लौटने। मुझे यकीन है कि इस WCF सेवा है जो इस प्रकार परिभाषित किया गया है के साथ एक विन्यास मुद्दा है कर रहा हूँ:

[OperationContract] 
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json)] 
void ReNotify(int applicationKey); 

web.config फ़ाइल में कोई प्रविष्टि नहीं कर रहे हैं तो मान लेते हैं कि सेवा डिफ़ॉल्ट कॉन्फ़िगरेशन उपयोग करता है।

उत्तर

4

मैं इसमें कोई विशेषज्ञ नहीं हूं, वास्तव में मुझे एक ही समस्या थी (किसी अन्य कारण से)। हालांकि, ऐसा लगता है कि डब्ल्यूसीएफ सेवाएं स्वाभाविक रूप से AJAX का समर्थन नहीं करती हैं और इसलिए आपके पास इसे सक्षम करने के लिए आपके web.config फ़ाइल में निम्न कोड होना चाहिए।

<system.serviceModel> 
    <behaviors> 
     <endpointBehaviors> 
      <behavior name="NAMESPACE.AjaxAspNetAjaxBehavior"> 
       <enableWebScript /> 
      </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" 
     multipleSiteBindingsEnabled="true" /> 
    <services> 
     <service name="NAMESPACE.SERVICECLASS"> 
      <endpoint address="" behaviorConfiguration="NAMESPACE.AjaxAspNetAjaxBehavior" 
       binding="webHttpBinding" contract="NAMESPACE.SERVICECLASS" /> 
     </service> 
    </services> 
</system.serviceModel> 

और उसके बाद इस सेवा वर्ग

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Activation; 
using System.ServiceModel.Web; 
using System.Text; 

namespace NAMESPACE 
{ 
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)] 
    [ServiceContract(Namespace = "")] 
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
    public class SERVICECLASS 
    { 
     // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json) 
     // To create an operation that returns XML, 
     //  add [WebGet(ResponseFormat=WebMessageFormat.Xml)], 
     //  and include the following line in the operation body: 
     //   WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml"; 
     [OperationContract] 
     public string DoWork() 
     { 
      // Add your operation implementation here 
      return "Success"; 
     } 

     // Add more operations here and mark them with [OperationContract] 
    } 
} 

यह है वी.एस. 2012 तक क्या जनरेट किया गया था जब मैं एक AJAX जोड़ा में WCF सेवा सक्षम होना चाहिए।

संबंधित मुद्दे