简体   繁体   English

跨域请求,但POST为空

[英]crossdomain request, but POST is empty

I need to implement a cross-domain POST request. 我需要实现跨域POST请求。 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: 我尝试使用ajax和JSONP发送POST请求:

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. 但是$ _POST为空。 But $_GET takes values $_POST. 但是$ _GET的取值为$ _POST。 If I check $_POST using var_dump, it is array(0), $_GET contains all send data! 如果我使用var_dump检查$ _POST,它是array(0),$ _ GET包含所有发送数据!

What's wrong here? 怎么了 What went wrong can it be? 可能出什么问题了?

您不能发出jsonp POST请求,只能使用GET完成jsonp,这就是为什么所有数据都在$ _GET中的原因。

JSONP doesn't work by POST request. JSONP不适用于POST请求。 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. JSONP的工作原理是:调用方生成一个带有url的脚本标签,并希望根据需要生成一些脚本,并使用传递的数据来调用回调函数。 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 http://remysharp.com/2011/04/21/getting-cors-working/ , but I confess, I didn't even fully scan it, I just looked over it to see that likely documents the basic, necessary functionality). 如果您想要真正的跨域发布,则必须实现跨域资源共享(CORS)功能,以处理一些OPTIONS请求(例如,使用http://remysharp.com/2011/04/21/getting-cors -working / ,但我承认,我什至没有完全扫描它,只是查看了一下,看是否可能记录了基本的必要功能)。

You have send JSON formatted since you are using jsonp format. 由于您使用的是jsonp格式,因此您已发送JSON格式的消息。 try json_encode before you echo data. echo数据之前尝试json_encode

Also keep in mind that when you make a cross domain jsonp call, JSONP isn't AJAX, it's merely a dynamic script element. 还请记住,当您进行跨域jsonp调用时,JSONP不是AJAX,它只是一个动态脚本元素。 You can't do a POST with a dynamic script element. 您不能使用动态脚本元素执行POST。 There's no place to put the POST data. 没有放置POST数据的地方。 So You will have to use GET method. 因此,您将不得不使用GET方法。

Also keep in mind that you can use below format 另外请记住,您可以使用以下格式

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

});

Read more 阅读更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM