I need to implement a cross-domain POST request. Using this code, everything worked fine in the same domain. But when I moved the backend to another domain - all stopped working! Therefore, there can be typos. There can be only where the error is related to the cross-domain request. I try send POST request using ajax and JSONP:
function requestToServer(url, success, myObjects)
{
$.ajax({
type: "POST",
crossDomain: true,
dataType: 'jsonp',
jsonp: 'jsonp_callback',
url: url,
data: "arrObjects=" + JSON.stringify(myObjects),
success: function(data)
{
success(data);
},
error: function()
{
alert('Server connection error!!!');
}
});
}
and server-script, where send data:
<?php
header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']);
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Content-Type');
include 'connection.php';
$arrObjects = json_decode($_POST['arrObjects']);
$title = $arrObjects->title;
$msg = $arrObjects->msg;
$lat = $arrObjects->lat;
$lon = $arrObjects->lon;
$query = "INSERT INTO `geo_markers` (`id`, `title`, `description`, `lat`, `lon`)
VALUES (NULL, '{$title}', '{$msg}', '{$lat}', '{$lon}')";
$res = mysqlQuery($query);
echo $_GET['jsonp_callback'].'({"success":true});';
mysql_close();
?>
but $_POST is empty. But $_GET takes values $_POST. If I check $_POST using var_dump, it is array(0), $_GET contains all send data!
What's wrong here? What went wrong can it be?