I am trying to pass a json data from mobile application through ajax call. My Ajax call is
$.ajax({
type: 'GET',
data: postData,
jsonp: "callback",
dataType: 'jsonp',
url: 'http://*****.*****.org.in/qr.php',
success: function(data){
$ionicLoading.hide();
console.log(data);
alert('Your data was successfully added');
$location.path('/home');
},
error: function(data,error){
$ionicLoading.hide();
console.log(error);
alert('There was an error adding your data');
}
});
The value in postData is
{ event_name: "Contraptions" event_type: "Engineering" participant_type: "Individual" place: "Third" qr1: "amsds" qr2: "" qr3: "" qr4: "" qr5: "" round: "3" teamname: ""}
I could see the postData json in console.
My server side Code qr.php is
<?
try
{
$callback = isset($_GET['callback']) ? preg_replace('/[^a-z0-9$_]/si', '', $_GET['callback']) : false;
header('Content-Type: ' . ($callback ? 'application/javascript' : 'application/json') . ';charset=UTF-8');
$json = json_decode('postData');
$data = array('status' => $json );
echo ($callback ? $callback . '(' : '') . json_encode($data) . ($callback ? ')' : '');
}
catch(Exception $e)
{
echo $e;
}
?>
But the $json which is decoded is NULL. How should I get the JSON data and decode that JSON that was passed from client ?????
Create a correct object in JavaScript
fi
var data = {event_name: "Contraptions", event_type: "Engineering", participant_type: "Individual"};
and use
data : {postData: JSON.stringify(data)};
in the ajax call.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.