2

@SOLVED

As explained by James M. Lay, I should change my content-type from application/x-www-form-urlencoded to application/json

it implied in an error because it seems that only UrlEnconded types generates POST arrays in server side (at least in PHP). So I had to change the way I receive/deal with the request in my server script

$json = file_get_contents('php://input'); //yes. php://input

if($json) $params = json_decode($json,true);
else $params = $_POST;

I also had to make a few changes in the Javascript code to check the content-type and generate different strings. If it's JSON I just use JSON.stringify

//string to use in the 'send' method
this.getParametersString = function(){
        if(this.contentType == 'application/json'){
            return JSON.stringify(this.parameters);
        }else{}
}

I got a question

I`m building a function that receive parameters to write a list of parameters and send it by POST

The problem is that we can't send special characters, such is +

So I tried to use the function encodeURIComponent to encode them to a URI friendly string.

There comes another problem: if the parameter received is an object, I am loop through the attributes, checking if it is another object or a string, if it is an object, loop again, otherwise encode it.

But it is returning an object of encoded strings. I have to make the object become a string to send it, and for that purpose I use JSON.stringify. It decodes the encoded string. So %2B becomes + again and It is not sent to the server via POST.

on the other hand If I use stringify first and the encodeURIComponent it generates signs like " and { } that shouldn't be encoded and the string is not a well written JSON

How do you that? Is that a way without using jQuery? Do I have to build my own stringify function?!

Victor Ferreira
  • 6,151
  • 13
  • 64
  • 120
  • Why not stringify all the params as one and send it? The POST format shouldn't be dependent on the arguments. `stringify` in JS and parse on server. – Rudie Aug 23 '14 at 21:38
  • 2
    Skip the `encodeURIComponent` altogether. JSON naturally can handle special characters, and JSON.stringify() will automatically take care of most edge cases for you. The reason you would want to `encode` your POST is if the server is expecting `application/x-www-form-urlencoded`. Since your server is expecting JSON, you can just send plain ol' JSON up the line. – James M. Lay Aug 23 '14 at 21:43
  • @JamesM.Lay maybe this is the point. `application/x-www-form-urlencoded` is what I have in my `Content-type`. do I need to change it!? I'll try – Victor Ferreira Aug 23 '14 at 21:55
  • The semantically correct content-type header here is `application/json`, but you can also use `text/plain`. – James M. Lay Aug 23 '14 at 21:56
  • @JamesM.Lay there seems to be a new problem. I changed my code and now It uses application/json as default content type. I can see special chars (just tested + plus sign) being sent in the headers, but it seems the server isn't being reached. I receive a blank response. but if I change back to `application/x-www-form-urlencoded`, it works fine (i receive a JSON string). got any idea of what is happening? – Victor Ferreira Aug 23 '14 at 22:32
  • Best thing to do in this situation is check the javascript console. Press F12 in most browsers, (command-i on Mac I think). It could be a CORS request error. – James M. Lay Aug 23 '14 at 22:36
  • yes I know that. blank response with application/json and JSON string (as expected) with application/x-www-form-urlencoded. the same happens if I use anything that isn't a valid content-Type. so it seems that application/json isn't working for some reason in my server (most likely) or browser – Victor Ferreira Aug 23 '14 at 22:42
  • [below link may help you ](http://stackoverflow.com/questions/19548395/jquery-ajax-post-request-with-special-characters) http://stackoverflow.com/questions/19548395/jquery-ajax-post-request-with-special-characters – Shaik Matheen Oct 25 '16 at 11:00

1 Answers1

9

im using the following and i have no issues

encodeURIComponent(JSON.stringify(object_to_be_serialised))
King Loui
  • 190
  • 9
  • 1
    when I have an object like `{number:1}` and I stringify it, it will generate a string as following {"number":1}. if escape this string it generates another string with quotes and curly brackets changed. but they shouldnt have been changed – Victor Ferreira Aug 23 '14 at 21:54