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'];
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?
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.
Related
Here is an excerpt from w3.org on http responses:
10.2 Successful 2xx
This class of status code indicates that the client's request was
successfully received, understood, and accepted.
10.2.1 200 OK
The request has succeeded. The information returned with the response is dependent on the method used in the request, for example:
GET an entity corresponding to the requested resource is sent in the response;
POST an entity describing or containing the result of the action;
Is it considered "received, understood, and accepted" when the $_POST[] variables are stored in some other variable?
EDIT: here is the ajax call which calls an empty php file.
$.ajax({
url: 'process.php',
data: 'type=new&title='+title+'&startdate='+start+'&zone='+zone,
type: 'POST',
dataType: 'json',
success: function(response){
event.id = response.eventid;
$('#calendar').fullCalendar('updateEvent',event);
},
error: function(e){
alert('Error processing your request: '+e.responseText);
}
});
and it calls the error part of the ajax call. But the console shows no errors. Now why it chooses only error and not success ??
You don't need to store anything... it's only about the response code.
For example I can make a POST to a "script" that doesn't process anything, just a dummy empty file...
I don't really understand what's your "dilemma".
I'm using a device detection PHP script on the server, mobiledetect.net and normally the user's browser would make a direct call to that on the server and so obviously the detection script would get all the HTTP headers (that it uses for device detection) directly that way.
If I call the same server side PHP detection script via a JQuery AJAX call from my javascript running on the user's browser, does it receive all the HTTP headers it needs for detection, as it would with the direct method? i.e. does JQuery allow or set all the HTTP headers for the AJAX call on the script as the browser would directly?
If not, how would I achieve this please?
Headers the detection script requires are: HTTP_ACCEPT, HTTP_X_WAP_PROFILE, HTTP_X_WAP_CLIENTID, HTTP_WAP_CONNECTION, HTTP_PROFILE, HTTP_X_OPERAMINI_PHONE_UA, HTTP_X_NOKIA_GATEWAY_ID, HTTP_X_ORANGE_ID, HTTP_X_VODAFONE_3GPDPCONTEXT, HTTP_X_HUAWEI_USERID, HTTP_UA_OS,
HTTP_X_MOBILE_GATEWAY, HTTP_X_ATT_DEVICEID, HTTP_UA_CPU
Many thanks.
Yes you can set the HTTP headers in your ajax call :
$.ajax({
url: 'YourRestEndPoint',
headers: {
'header1':'xxxxxxxxxxxxx',
'herader2':'xxxxxxxxxxxxxxxxxxxx',
'Content-Type':'application/json'
},
method: 'POST',
dataType: 'json',
data: YourData,
success: function(data){
console.log('succes: '+data);
}
});
Please find more information here :Add Header in AJAX Request with jQuery.
To get the value of the HTTP headers, use the following code :
var req = new XMLHttpRequest();
req.open('GET', document.location, false);
req.send(null);
var headers = req.getAllResponseHeaders().toLowerCase();
alert(headers);
More information here
I'm having some trouble using $http.post() with angularJS.
When I use the "post" method with a form, I can find the data in my php file using $_POST, and it works fine.
But when I do, for example:
$http.post(myUrl, {data: "test data"})
.success(function(){
console.log("ok");
});
How can I get the data and use it in my php file?
Thanks
By default, Angular $http will use the header Content-type: application/json and send the request body as JSON. You can get this in PHP using:
json_decode(file_get_contents("php://input"));
More information about php://input
If you don't want this and you want to use $_POST you will have to send the data as x-www-form-urlencoded which requires changing the header and sending the data as an x-www-form-urlencoded string. For example:
$http({
url: myUrl,
data: "data=" + encodeURIComponent("test data"),
method: "POST",
headers: {"Content-type": "application/x-www-form-urlencoded"}
});
If you want these settings to be global, you can configure them with $httpProvider.defaults
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?
You don't need to do contentType: "application/json", when you don't specify content type, it converts the data to be sent in http params, from the json..which are accessible via $_GET, or $_POST params..
But if you want to send json data only..you can try this code on server side to get the data:
<?php
$data = #file_get_contents('php://input');
print_r(json_decode($data));
?>
You were not sending JSON data back. jQuery considers it an error when the ajax content type is set to accept JSON, but malformed JSON is sent back.
echo $_POST['data'] will likely throw the exception "Catchable fatal error: Object of class stdClass could not be converted to string" -- so that actually gets printed out. That's not valid JSON.
What you probably want to do is echo json_encode($_POST['data']);
I am making an ajax request from a jquery script to one of my ajax controllers in codeigniter. This process works fine when using data type json. However, when I want to send xml back from the server, the jquery documentation says that the server must specify a MIME type of text/xml. How do I do this with codeigniter.
My ajax call looks like this:
$.ajax({
type: "POST",
url: siteUrl + "index.php/ajax_controller/test",
dataType: "xml",
success: testSuccess
});
You can specify a content type header using the following code. You could put it at the top of your controller method in CI:
header("Content-Type: text/xml");
It must be before any output starts, so use it before you call your first $this->load->view().