简体   繁体   English

接收用ajax发布的数据

[英]Receive data posted with ajax

I have this code for sending data with ajax to a update.php page我有使用ajax发送数据到update.php页面的代码

$(document).ready(function() {
  $("#modify").click(function() {
    var a = $("#a").val();
    var b = $("#b").val();
    var c = $("#c").val();
    $.ajax({
      type: "POST",
      data: {
        a: 'a',
        b: 'b',
        c: 'c',
        id: 'id'
      },
      url: "update.php",
      success: function(result) {

      }
    });
  });
});

In the update page, I receive data like this在更新页面,我收到这样的数据

id = $_POST["id"];
a = $_POST["a"];
b = $_POST["b"];
c = $_POST["c"];

Is it correct or does it have a problem, because that doesn't work.它是正确的还是有问题,因为那不起作用。

That works very well, but i think you wrote the strings instead in the variables 'data' object.这很有效,但我认为您在变量“数据”对象中编写了字符串。

 var a = $("#a").val();
    var b = $("#b").val();
    var c = $("#c").val();
    $.ajax({
      type: "POST",
      data: {
        a: a,
        b: b,
        c: c,
        id: id
      },

I checked your code and it worked well, I suggest check the url again我检查了你的代码,它运行良好,我建议再次检查 url

a good job for finding error is adding console.log() to the success part, like this:发现错误的一个好工作是将 console.log() 添加到成功部分,如下所示:

     url: "update.php", //check it 
      success: function(result) {
           console.log(result);

         // or you can do this instead of console.log(result)
        // alert(result)

       }

and also put this state in your php code :并将此状态放入您的 php 代码中:

<?php
   print_r($_POST);
?>

then after clicking on the button you can see the result, on the console of browser然后单击按钮后,您可以在浏览器的控制台上看到结果

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

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