I'm using jquery ajax function. I noticed an issue when I post JSON data to the server.
The type of post data is JSON. So I added the code to specify what I sent was JSON.
contentType: "application/json".
I wrote below code:
var data = {"data": "mytestdata" };
var option = {
url: 'Handler1.ashx',
type: 'POST',
dataType: 'html',
success: function (result) {
alert(result);
},
data: data,
contentType: "application/json"
};
$.ajax(option);
At the server side, I used below code:
string s = context.Request["data"];
But the result s
was null
.
Logically,setting contentType="application/json"
and posting json data are perfect. But it's false.
Also I tried code in php file:
echo $_POST["data"];
PHP says $_POST["data"] doesn't exist
.
So I tried to remove the code -- contentType: "application/json"
.
Now,everything is OK.
But it confused me. Why needn't set contentType as json when we post the real json data?