I have specified Ajax response on server side as:
header("Content-Type: application/json; charset=utf-8");
When I check after my request completion, I get the following response header:
Content-type:text/html; charset=UTF-8
In my response, I am sending the following status based on whether the request got completed successfully or failed or there is an exception.
if($stmt->rowCount() == 0) {
// Create SQL Statement
foreach ($payload_jsonDecoded as $i => $item) {
$sql = $sql . "some insert query";
}
// Prepare statement
$insertstmt = $conn->prepare($sql);
// execute the query
$insertstmt->execute();
if($insertstmt->rowCount() > 0){
$json = json_encode(array(
"status" => true,
));
}
else{
$json = json_encode(array(
"status" => false,
));
}
}
else{
$json = json_encode(array(
"status" => false,
));
}
echo $json;
die();
On client side, I am using:
$.ajax({
url: 'path',
type: "POST",
data: jsonObj,
//dataType: "jsonp",
processData: false,
contentType: "application/json; charset=utf-8",
success: function (output, status, xhr) {
console.log(xhr.getResponseHeader("Content-Type"));
My logic as well as code works, but on the client side, my response contains the following messages which messes up the response message and hence I am not able to display correct message to the end users.
Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unknown on line 0
Warning: Cannot modify header information - headers already sent in Unknown on line 0
Any insights would be grateful.
答案 0 :(得分:1)
Use the php header
header("Content-Type: application/json");
Before you actually echo the content
*i see you use this already but it should work