我想通过jquery.post
将对象列表发布到我的WCF
方法。我的其他方法都很有效。但是,将数据插入数据库表并接受数据为通用列表的一种方法会导致内部服务器错误。虽然我已经用我的consoletest
客户端测试了这个插入方法,它也工作得很好。但只有得到问题与jQuery的帖子。可能是我的JSON对象不是必需的格式。
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>
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);
}
});