views:

48

answers:

2

I tried to send the request from jquery ajax with contentType as 'text/plain'. I am unable to access the values on the server side. I am accessing the values using $_POST array in php file. Why is this happening.

jQuery AJAX code:

$.ajax({ 
    type: "POST", 
    data: {o_pass:o_pass,n_pass:n_pass}, 
    url: "changepass", 
    success: function(response) { alert(response); } 
});

Server side:

$old_pass = $_POST['o_pass']; 
$new_pass = $_POST['n_pass'];
+1  A: 

Because POST requests should have a content type of application/x-www-form-urlencoded or multipart/form-data so that the server knows what it is dealing with.

What is the reason for sending the request as plain text?

Mark B
I am unable to send the data with ' ' or '+'. They get changed on the server side, space -> plus and plus -> space. So, i was trying to send data as plain text.
Aashish
Normal POST requests will handle these characters without issue. Please post your jQuery ajax code so we can see how you are performing the request.
Mark B
The jQuer Ajax call is like this : $.ajax({ type: "POST", data: {o_pass:o_pass,n_pass:n_pass}, url: "changepass", success: function(response) { var result = eval(response); alert(result[1]); window.location = "/my_profile"; } });And on server side i am accepting values like:$old_pass = $_POST['o_pass'];$new_pass = $_POST['n_pass'];
Aashish
Well, I'm sorry then but I'm not sure—I just tried your code with both the `o_pass` and `n_pass` Javascript variables set to `test+ saf`, and it POSTed them both with no problems. I suspect there is something else going on here but you need to provide MUCH more detail in your questions. I've edited this one to include your code now, maybe someone else can help.
Mark B
Aashish
A: 

You shouldn't have to worry about the content type, when doing a standard post request.

Try changing your url: changepass to changepass.php. You probably have an html or htm file named changepass that your server is processing your post request.

Gutzofter