hope you're all doing well. Here's the deal. I make an AJAX call to PHP and PHP decodes the JSON string then echoes a property from the json object but in the AJAX response alert, I get the correct value from the json property AND the source of the current page, for example:
jsonProperty<!DOCTYPE HTML>
<html>
<head>... [the rest of the page's source]
Here is my code: PHP
<?php
private function validate_review(){
$json = json_decode($_POST['data']);
echo $json->review;
}
?>
AJAX:
<script>
var reviewData = {
title : $('#fieldtitle').val(),
raiting : starRaiting,
review : $('#fieldreview').val()
}
$.ajax({
type: 'post',
url: 'http://localhost/codeigniter/new-review',
data: {data: JSON.stringify(reviewData)},
success: function(result){
alert(result);
}
});
</script>
Why would the response also include the source of the page, this is completely counter-intuitive and strange. Help?
I think your code still includes the template.
private function validate_review(){
$json = json_decode($_POST['data']);
die($json->review);
}
This should work.
specify your request dataType
$.ajax({
type: 'post',
url: 'http://localhost/codeigniter/new-review',
data: {data: JSON.stringify(reviewData)},
dataType: 'jsonp', //tell the server that you expect json
success: function(result){
alert(result);
}
});
<?php
//DO NOT echo any output before header
header('Content-Type: application/json'); //said this response content is json
?>
and die instead of echo json content