我正在使用此功能:
function end_incident() {
var dataString = 'name=Daniel&phone=01234123456';
$.ajax({
type: "POST",
url: "http://www.example.co.uk/erc/end_incident.php",
data: dataString,
success: function(msg){
alert('Success!'+dataString);
}
});
};
向end_incident.php
发送信息,但我无法访问$_POST
个变量。我试过这样做:
$name = $_POST['name'];
$phone = $_POST['phone'];
我做错了吗?
感谢您的帮助
答案 0 :(得分:13)
尝试将数据作为对象发送:
function end_incident() {
$.ajax({
type: "POST",
url: "http://www.example.co.uk/erc/end_incident.php",
data: { name: "Daniel", phone: "01234123456" },
success: function(msg){
alert('Success!');
}
});
};
答案 1 :(得分:3)
确保您请求的网址位于您网站的同一个内,如果不是,则会遇到跨网站脚本问题。只有这样:
通过您自己的网站使用代理来获取文件请求:
var getURL = "http://www.example.co.uk/erc/end_incident.php";
$.ajax({
type: "POST",
url: "/get_url.php?url=" + encodeURIComponent(getURL),
data: { name: "Daniel", phone: "01234123456" },
success: function(msg){
alert('Success!');
}
});
我建议您在ajax中添加error
功能。令人惊讶的是,有多少人专注于success
并且从不处理错误!
error: function()
{
console.log(arguments);
}