我正在创建一个 WCF Restful 服务,我想通过 POST 操作将来自 javascript 的 json 对象保存为查询字符串。我将 json 对象作为查询字符串获取,但无法在 wcf restful 中访问它,请帮助.... ……
我的 JavaScript 代码
var myRequest = new XMLHttpRequest();
myRequest.onreadystatechange=function(dataString) {
if (myRequest.readyState == 4 && myRequest.status == 200) {
console.log('Sent to server: ' + dataString + '');
window.localStorage.removeItem(dataString);
}
else if (myRequest.readyState == 4 && myRequest.status != 200) {
console.log('Server request could not be completed');
saveDataLocally(dataString);
}
}
var url="http://localhost:58168/RestServiceImpl.svc/json";
myRequest.open("POST",url+"?"+dataString,true);
myRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
myRequest.send(dataString);
alert('Saved to server: ' + dataString + '');
我在 javascript 中的请求
![同时将 json 对象作为查询字符串发送到 wcf restful 服务器][1]
>**RequestURL**:http://localhost:58168/RestServiceImpl.svc/json?"firstName":"shuresh","lastName":"kumar"}
请求标头: Content-Type:application/x-www-form-urlencoded 来源:chrome-extension://ddijiilbgbjgciahjmonfahapadmkcfp User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/27.0。 1453.116 Safari/537.36
查询字符串参数 {"firstName":"shuresh","lastName":"kumar"}:
表单数据 {"firstName":"shuresh","lastName":"kumar"}:
WCF Restful 服务合约
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json?dataString={dataString}")]
string JSONData(string dataString);
RestServiceImpl.svc.cs
public string JSONData(string dataString)
{
return "You requested product " + dataString;
}
我无法访问服务合同中的 json 对象(dataString)。请帮助我如何访问附加在上述 wcf restful 代码中的查询字符串中的 json 对象。
http://localhost:58168/RestServiceImpl.svc/json?"firstName":"shuresh","lastName":"kumar"}