使用wcf Restful Service将json对象作为查询字符串 [英] Getting json object as querystring using wcf Restful Service

查看:118
本文介绍了使用wcf Restful Service将json对象作为查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个WCF Restful服务,我希望通过POST操作保存来自javascript的json对象作为查询字符串。我将json对象作为查询字符串但是无法在wcf restful中访问它,请帮助..........



Hi , i am creating a WCF Restful service in which i want save the json object coming from the javascript as querystring by POST operation..I am getting the json object as querystring but can't access it in wcf restful, please help..........

My javascript code 







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 server]




My Request in javascript
![while sending the json object as querystring to wcf restful server]

RequestURL:http://localhost:58168RestServiceImpl.svcjson?"firstName":"shuresh","lastName":"kumar"}

Request Header: Content-Type:application/x-www-form-urlencoded Origin: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

Query String Parameters {"firstName":"shuresh","lastName":"kumar"}

Form Data {"firstName":"shuresh","lastName":"kumar"}:





WCF休息服务合同



WCF Restful service contract

[OperationContract]
        [WebInvoke(Method = "POST",
            ResponseFormat = WebMessageFormat.Json,
             RequestFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "json?dataString={dataString}")]
        string JSONData(string dataString);





RestServiceImpl.svc.cs





RestServiceImpl.svc.cs

public string JSONData(string dataString)
       {

           return "You requested product " + dataString;


       }





我无法访问json对象(dataString )在服务合同中。请帮助我如何访问wcf restful上面代码中的查询字符串中附加的json对象。





I can't access the json object(dataString) in the service contract.please help me how to access the json object that is append in the querystring in the wcf restful above code.

http://localhost:58168/RestServiceImpl.svc/json?"firstName":"shuresh","lastName":"kumar"}

推荐答案

试试这个带有JSON的WCF REST服务 [ ^ ]


这篇关于使用wcf Restful Service将json对象作为查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆