Click here to Skip to main content
16,017,881 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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 


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 + '');

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 Restful service contract
C#
[OperationContract]
        [WebInvoke(Method = "POST",
            ResponseFormat = WebMessageFormat.Json,
             RequestFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "json?dataString={dataString}")]
        string JSONData(string dataString);


RestServiceImpl.svc.cs

C#
public string JSONData(string dataString)
       {

           return "You requested product " + dataString;


       }


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"}
Posted
Updated 9-Jul-13 3:14am
v2

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900