我正在使用 jquery $.post
向另一个域发送 post 请求。一切正常,但我没有在请求的页面中获取发布的数据,请检查我的代码
jquery代码
var data = {mydata: 'testing'};
$.post("http://anyurl/file.php",data,function(info){
alert(info);
});
这是 php 代码
<?php
header('Access-Control-Allow-Origin: *'); // this is to allow another domain
$data = $_POST[“mydata”]; // assigning data to variable
echo $data; // sending back to jquery
?>
它不返回数据,请检查任何人。
提前致谢
最佳答案
使用正确的引号
并使用isset()
来检查数据
集是否相同,
<?php
header('Access-Control-Allow-Origin: *'); // this is to allow another domain
print_r($_POST);// to check the post data
$data = isset($_POST['mydata']) ? $_POST['mydata'] : "No Data";
echo $data; // sending back to jquery
?>
关于javascript - php post 无法使用 header 和 ajax 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19970279/