Tuesday, 3 January 2017

json - Remove Escape character from object or string in c#?



I am passing string, through ajax, to action method in mvc,
Below is my ajax call




                $("#btnEncrypt").click(function () {
var pay = JSON.stringify({
'payload': {"exp":1442515543,"method":"Login"},
'secretKey': "123456"
});

$.ajax({
type: "POST",
url: '@Url.RouteUrl("GetData")',

data: pay,
contentType: 'application/json; charset=utf-8',
success: function (data) {
alert("Success");
},
error: function () {
alert('error');
}
});
});



Below is my Action method(Generate) in which I had pass two parameters



                [HttpPost]
public ActionResult Generate(string payload, string secretKey)
{
Here I am getting payload as "{\"exp\":1442515543,\"method\":\"Login\"}"
instead of "{"exp":1442515543,"method":"Login"}"
}



So, how can I get original request that I am passing through ajax call



    "{"exp":1442515543,"method":"Login"}" 


in action method without escaping character? Thank you in advance :)



Note :

1) I had tried without stringify for request parameter, but it's not worked.
2) Here parameters(exp and method) vary according to different request and I had around 200 request which all are using different paramters.


Answer



Here just need to Deserialize the payload at Action to remove escape charactor



var obj = new JavaScriptSerializer().Deserialize(payload,targetType:null);


Problem solved :)


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...