[英]Passing JSON to WCF via Ajax Call
我在網上搜尋了有關如何正確將JSON字符串正確發送到WCF服務的說明。 我的應用程序有12個GET,它們都可以正常運行,但是我無法通過POST進入調試器。 我已經煮到最簡單的JSON字符串,但在chrome瀏覽器中仍然出現400錯誤。
請看下面...
JS:
workDataAsJson = JSON.stringify('{"TestData":"121"}');
$.ajax({
type: "POST",
async: true,
cache: false,
timeout: webCallDefaultTimeout,
contentType: "application/json; charset=utf-8",
url: baseUrl.concat('UpsertWorkData/' + workDataAsJson),
dataType: "json",
success: function (response, status, jqXHR) {
if (status == 'success') {
var workplanData = $.parseJSON(response);
// notify user of success,...
} else {
displayGenericModal('Web Service Error', 'Uh Oh! Unable to Connect to the Database to Obtain Work Data');
}
},
error: function (response, status, jqXHR) {
displayGenericModal('Web Service Error', 'Uh Oh! Unable to Connect to the Database to Obtain Work Data');
}
});
} catch(ex) {
alert(ex);
}
現在,對於WCF運營合同,...
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, Method = "POST", UriTemplate = "UpsertWorkData/{WorkData}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string UpsertWorkData(string WorkData);
好的,現在是web.config文件。 別笑,我基本上已經把我讀過的所有東西都扔了!
<?xml version="1.0"?>
<configuration>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483644"/>
</webServices>
</scripting>
</system.web.extensions>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
</appSettings>
<!--
For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367.
The following attributes can be set on the <httpRuntime> tag.
<system.Web>
<httpRuntime targetFramework="4.5" />
</system.Web>
-->
<system.web>
<compilation debug="true" targetFramework="4.5"/>
<httpRuntime targetFramework="4.5" maxRequestLength ="798778" maxUrlLength="779779" enable="true"/>
</system.web>
<system.serviceModel>
<services>
<service name="WorkDataService.Service1">
<endpoint address="" binding="webHttpBinding" bindingConfiguration="" behaviorConfiguration="restfulBehaviour" name="ServicesEndpoint" contract="WorkDataService.IWorkDataService" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="restfulBehaviour">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="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="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="webHttpBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
<bindings>
<webHttpBinding>
<binding maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="200" maxStringContentLength="83886089" maxArrayLength="163841" maxBytesPerRead="2147483647" maxNameTableCharCount="16384"/>
</binding>
</webHttpBinding>
</bindings>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
救命!! (或者我必須使用節點!:)
問題是您正在嘗試對已經為字符串格式的數據進行字符串化。 刪除JSON.stringify
workDataAsJson = '{"TestData":"121"}';
要么
workDataAsJson = JSON.stringify({"TestData":"121"});
另外uri應該更改為
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "/UpsertWorkData",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
string UpsertWorkData(string WorkData);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.