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

发布于 2024-11-07 21:00:49 字数 537 浏览 0 评论 0原文

我正在使用此函数:

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

我做错了什么吗?

感谢您的帮助

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. I've tried doing it like this:

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

Am I doing something wrong?

Thanks for any help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初与友歌 2024-11-14 21:00:49

尝试将数据作为对象发送:

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

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!');
       }
    });
};
沦落红尘 2024-11-14 21:00:49

确保您请求的网址与您的网站同源,如果不是,则说明您遇到了跨站点脚本问题。唯一的解决方法是:

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

    var getURL = "http://www.example.co.uk/erc/end_incident.php";
    $.ajax({
       类型:“帖子”,
       url: "/get_url.php?url=" +encodeURIComponent(getURL),
       数据:{姓名:“丹尼尔”,电话:“01234123456”},
       成功:函数(消息){ 
           警报('成功!');
       }
    });
    

我建议您向 ajax 添加一个 error 函数。令人惊讶的是,有多少人只关注成功而从不处理错误!

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

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, i.e. create an add-on/extension, or use 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. It's suprising how many people just focus on success and never process an error!

error: function()
{
   console.log(arguments);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文