访问Jquery / AJAX发送的$ _POST数据

时间:2011-05-17 08:26:28

标签: php javascript jquery ajax post

我正在使用此功能:

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'];

我做错了吗?

感谢您的帮助

2 个答案:

答案 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)

确保您请求的网址位于您网站的同一个内,如果不是,则会遇到跨网站脚本问题。只有这样:

  • 在浏览器中获得“更高”的访问权限/ priveledges,即创建附加组件/扩展程序,或使用Greasemonkey
  • 通过您自己的网站使用代理来获取文件请求:

    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);
}
相关问题