我想通过WCF
将对象列表发布到我的jquery.post
方法。我的其他方法工作正常。但是,将数据插入数据库表并接受数据作为通用列表的一种方法是给出内部服务器错误。虽然我已经使用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>
答案 0 :(得分: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);
}
});