简体   繁体   English

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

[英]Accessing $_POST data sent by Jquery/AJAX

I am using this function: 我正在使用这个功能:

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

to send information to end_incident.php , but I'm not able to access the $_POST variables. 将信息发送到end_incident.php ,但我无法访问$_POST变量。 I've tried doing it like this: 我试过这样做:

$name = $_POST['name'];
$phone = $_POST['phone'];

Am I doing something wrong? 难道我做错了什么?

Thanks for any help 谢谢你的帮助

Try sending the data as an object: 尝试将数据作为对象发送:

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

Make sure the url your requesting for is within the same origin of your site, if it isn't, you've got a cross-site scripting issue. 确保您请求的网址与您网站的同一来源相同 ,如果不是,您就会遇到跨网站脚本问题。 Only way around that: 只有这样:

  • Getting "higher" access/priveledges within the browser, ie create an add-on/extension, or use Greasemonkey 在浏览器中获得“更高”的访问/权限,即创建附加/扩展,或使用Greasemonkey
  • Use a proxy through your own site to get the request for the file: 通过您自己的站点使用代理来获取文件请求:

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

I recommend you add a error function to your ajax. 我建议你为你的ajax添加一个error函数。 It's suprising how many people just focus on success and never process an error! 令人惊讶的是,有多少人专注于success而从不处理错误!

error: function()
{
   console.log(arguments);
}

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

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