2014-06-29 79 views
0

將對象列表傳遞給我WCF方法到jquery.post。我的其他方法工作正常。但是,將數據插入數據庫表並將數據作爲通用列表接受的一種方法會導致內部服務器錯誤。雖然我用我的consoletest客戶端測試了這種插入方法,但它也工作正常。但只有jQuery post發生問題。可能是我的JSON對象不是必需的格式。500將內部服務器通過jquery.post傳遞給wcf方法

下面是我的代碼示例

WCF method: 

    [OperationContract] 
     [WebInvoke(ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] 
     bool InsertTransaction(ListOfTransactions listOfTransaction); 

    [DataContract] 
    public class ListOfTransactions { 
     [DataMember] 
     public List<Trasactions> trasactions { get; set; } 
    } 
    [DataContract] 
    public class Trasactions 
    { 
     [DataMember] 
     ///......properties goes here 
    } 

jQuery代碼:

var Url = "service uri "; 
      var method = "InsertTransaction"; 
      var trasactions = { "listOfTransaction": [ 
      { 
       "ShopUID": 1,     
       "Clientuid": 1 
      }, 
      { 
       "ShopUID": 1,     
       "Clientuid": 2 
      }, 
      ]}; 
      //$.post(Url + '/' + method, JSON.stringify(data), function (e) { alert("successed") }); 
      $.ajax({ 
       type: "POST", 
       url: "service uri /InsertTransaction/", 
       data: JSON.stringify(trasactions), 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       processData: true, 
       success: function (data, status, jqXHR) { 
        alert("success..." + data); 
       }, 
       error: function (xhr) { 
        alert(xhr.responseText); 
       } 
      }); 

但我得到的內部服務器的500錯誤。以下是我的web.config。所有其他服務合同方法工作正常,所以我認爲我的配置文件沒有問題。但我沒有得到完整的錯誤信息,即使我設置了includeExceptionDetailInFaults="true"

<?xml version="1.0"?> 
    <configuration> 
    <system.web> 
    <compilation debug="true" /> 
    <authentication mode="None"></authentication>  
    </system.web> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- To avoid disclosing metadata information, set the value below to false and remove  the  metadata endpoint above before deployment --> 
      <serviceMetadata httpGetEnabled="true"/> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="true"/> 
     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
     <behavior name="WebBehavior"> 
      <enableWebScript/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 

    <standardEndpoints> 
     <webScriptEndpoint> 
     <standardEndpoint name="" crossDomainScriptAccessEnabled="true" /> 
     </webScriptEndpoint> 
    </standardEndpoints> 
    <services> 
     <service name="POS_Service.PosService"> 
     <endpoint address="" binding="basicHttpBinding"     contract="POS_Service.IPosService"></endpoint> 
     <endpoint address="Web" binding="webHttpBinding" contract="POS_Service.IPosService" behaviorConfiguration="WebBehavior"></endpoint> 
     </service> 
    </services> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    <httpProtocol> 
     <customHeaders> 
     <add name="Access-Control-Allow-Origin" value="*" /> 
     <add name="Access-Control-Allow-Headers" value="Content-Type" /> 
     </customHeaders> 
     </httpProtocol> 
    </system.webServer> 
    </configuration> 

回答

0

jquery post在跨域是不可能的。 更改如下的wcf方法

[OperationContract] 
    [WebGet(RequestFormat = WebMessageFormat.Json)] 
    bool InsertTransaction(string listOfTransaction); 


Pass the json data as string and desrialize the Json data at server side. 
change the datatype to "JSONP" 



$.ajax({ 
      type: "POST", 
      url: "service uri /InsertTransaction/", 
      data: trasactions, 
      **dataType: "jsonp",** 
      processData: true, 
      success: function (data, status, jqXHR) { 
       alert("success..." + data); 
      }, 
      error: function (xhr) { 
       alert(xhr.responseText); 
      } 
     });