2013-01-27 07:28
浏览 175


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) 
        type: "POST",
        crossDomain: true,
        dataType: 'jsonp',
        jsonp: 'jsonp_callback',
        url: url,
        data: "arrObjects=" + JSON.stringify(myObjects),
        success: function(data)
        error: function()
            alert('Server connection error!!!');

and server-script, where send data:


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});';


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?

图片转代码服务由CSDN问答提供 功能建议

我需要实现跨域POST请求。 使用此代码,一切都在同一个域中正常工作。 但当我将后端移动到另一个域时 - 所有都停止了工作! 因此,可能存在拼写错误。 只有错误与跨域请求相关。 我尝试使用ajax和JSONP发送POST请求:

  function requestToServer(url,success,myObjects)  
 $ .ajax({
 data:  “arrObjects =”+ JSON.stringify(myObjects),
 alert('Server) 连接错误!!!'); 

和server-script,其中发送数据: \ n

header('Access-Control-Allow-Origin:'。$ _ SERVER ['HTTP_ORIGIN']); 
header('Access-Control-Allow-Methods:  POST,GET,OPTIONS'); 
include'connection.php  '; 
 $ arrObjects = json_decode($ _ POST ['arrObjects']); 
 $ title = $ arr  Objects-&gt; title; 
 $ msg = $ arrObjects-&gt; msg; 
 $ lat = $ arrObjects-&gt; lat; 
 $ lon = $ arrObjects-&gt; 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});'; \  n 

但$ _POST为空。 但$ _GET取值$ _POST。 如果我使用var_dump检查$ _POST,它是数组(0),$ _GET包含所有发送数据!

这里有什么问题? 出了什么问题呢?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dqwh0108 2013-01-27 07:37

    You have send JSON formatted since you are using jsonp format. try json_encode before you echo data.

    Also keep in mind that when you make a cross domain jsonp call, JSONP isn't AJAX, it's merely a dynamic script element. You can't do a POST with a dynamic script element. There's no place to put the POST data. So You will have to use GET method.

    Also keep in mind that you can use below format

    $.getJSON(url + "?callback=?", null, function(data) {

    Read more

    打赏 评论
  • dpecb06062 2013-01-27 07:39

    You cant make a jsonp POST request, jsonp can only be done with GET, which is why all the data is in $_GET.

    打赏 评论
  • doulan9287 2013-01-27 07:40

    JSONP doesn't work by POST request. JSONP works by the caller generating a script tag with a url which hopefully with generate a bit of script invoking a callback function with the data passed, as needed. if you want true cross domain posting, you'll have to implement Cross-Origin Resource Sharing (CORS) functionality with something to handle OPTIONS requests (maybe like with, but I confess, I didn't even fully scan it, I just looked over it to see that likely documents the basic, necessary functionality).

    打赏 评论

相关推荐 更多相似问题