0

I have a question regarding to ajax,

I have something like:

ajax({
  type: 'post',
  data: {
    'mode': 'update',
    'data': JSON.stringify(jsonData)
  },
  url: '...',
  dataType: 'JSON', 
  success: function(res) {
    //...
  },
  error: function(res) {
    //...
  }
});

But I do not use contentType, but my request actually contain JSON and one extra info of 'mode', should I add some information here for contentType, I tested my calls, they are working without issues, but I would like to know if there is any issues with this call.

any help would be much appreciated.

Phil
  • 157,677
  • 23
  • 242
  • 245
  • is that `jQuery` ? – ThS Apr 05 '22 at 23:35
  • yes, it is jQuery. – user16542536 Apr 05 '22 at 23:39
  • in most cases `jQuery` will handle the `contentType` for you. The `contentType` is used to specify the data being sent to the server and in your case, `jQuery` *seems* to set that to `application/x-www-form-urlencoded; charset=UTF-8`. See [jQuery Ajax Docs](https://api.jquery.com/jquery.ajax/) and serach for `contentType` section. – ThS Apr 05 '22 at 23:46
  • If it aint broke... why are you concerned? If your code works fine... – Kinglish Apr 05 '22 at 23:49
  • I was new to these calls, while I am checking something else, I found this info contentType, so I was curious, what happened if I mixed up the request content here, part of it is Json. – user16542536 Apr 05 '22 at 23:53
  • Your request payload is `application/x-www-form-urlencoded`. The fact that one of the values is a JSON string is irrelevant. It will be encoded correctly for the request and your server-side code can retrieve it and parse it as JSON – Phil Apr 05 '22 at 23:55

1 Answers1

0

The issue is that your entire data object is not stringified..

try...


var data = {
    mode : 'update',
    data : jsonData
}

$.ajax({
    type: 'post',
    data: JSON.stringify(data),
    url: '...',
    contentType: 'application\json',
    success: function(res){...},
    error: function(res){...}
});

//also might want to try and use the short hand version for post
$.post(url, JSON.stringify(data))
    .done(function(res) { /**/ })
    .fail(function(res) { /**/ });

This solution stringifies the entire data object not just the one property.

Zach Painter
  • 180
  • 1
  • 9
  • OP said their code works just fine, and anyways, you don't have to stringify POST payload. You can hand jQuery the object and it will work with that. – Kinglish Apr 05 '22 at 23:48
  • 1
    I have experienced issues when trying to deserialize the request in asp.net core applications.. This is how I do it in my apps.. Although I do not use success and error options on ajax settings object. instead I use done and fail.. – Zach Painter Apr 05 '22 at 23:50
  • Yes, only part of my calls with JSON.stringify, that is why I have this question here, but Ajax did not give me any issues, so I assume it will be fine? I had many calls with this style already... I was new to Ajax and was not expecting contentType info. In my backend, I only decode the json part. – user16542536 Apr 05 '22 at 23:51
  • The question is what is your backend and how does it deserialize the request? Is this asp.net core mvc? – Zach Painter Apr 05 '22 at 23:56
  • @ZachPainter even if it was ASP.NET, if it's expecting `application/x-www-form-urlencoded` (which from OP's question, **it is**), sending `application/json` like in your answer will cause the request to fail – Phil Apr 05 '22 at 23:59
  • I use php, backend like: $data = json_decode(htmml_entity_decode(trim(htmlspecialchars($_POST["data"]))),TRUE); Then I can gather data like $fieldA = $data["fieldA"]; $fieldB = $data["fieldB"]; – user16542536 Apr 06 '22 at 00:00
  • @Phil I see.. you are right.. the call should work just fine then.. But the OP will need to make a special call or action to deserialize the json part of the request if desired. Otherwise it will just be a long string. – Zach Painter Apr 06 '22 at 00:01
  • json_decode works.. :) – Zach Painter Apr 06 '22 at 00:02
  • thanks for all the helps, so seems if I do not declear contentType, by defualt "application/x-www-form-urlencoded", you can mix json string with other parameters. – user16542536 Apr 06 '22 at 00:04
  • @user16542536 you don't need all that extra rubbish... `$data = json_decode($_POST['data'], true);`. See [this answer](https://stackoverflow.com/a/38411974/283366) – Phil Apr 06 '22 at 00:04
  • yep.. that seems right to me.. – Zach Painter Apr 06 '22 at 00:04
  • Cool, I will apply this to any new Ajax calls, just like $data = json_decode($_POST['data'], true); Thanks again!! – user16542536 Apr 06 '22 at 00:07