dpub33855 2018-01-25 02:45
浏览 75
已采纳

Ajax和PHP,发布请求不起作用

So I am trying to post some some data from one PHP file to another PHP file using jquery/ajax. The following code shows a function which takes takes data from a specific div that is clicked on, and I attempt to make an ajax post request to the PHP file I want to send to.

 $(function (){
              $(".commit").on('click',function(){
                const sha_id = $(this).data("sha");
                const sha_obj = JSON.stringify({"sha": sha_id});
                $.ajax({
                   url:'commitInfo.php',
                   type:'POST',
                   data: sha_obj,
                   dataType: 'application/json',
                   success:function(response){
                      console.log(response);
                      window.location.replace("commitInfo");
                    },
                   error: function (resp, xhr, ajaxOptions, thrownError) {
                        console.log(resp);
                     }
                 });
              });
           });

Then on inside the other php file 'commitInfo.php' I attempt to grab/print the data using the following code:

$sha_data = $_POST['sha'];
echo $sha_data;
print_r($_POST);

However, nothing works. I do not get a printout, and the $_POST array is empty. Could it be because I am changing the page view to the commitInfo.php page on click and it is going to the page before the data is being posted? (some weird aync issue?). Or something else? I have tried multiple variations of everything yet nothing truly works. I have tried using 'method' instead of 'type', I have tried sending dataType 'text' instead of 'json'. I really don't know what the issue is. Also I am running my apache server on my local mac with 'sudo apachectl start' and running it in the browser as 'http://localhost/kanopy/kanopy.php' && 'http://localhost/kanopy/commitInfo.php'.

Also, when I send it as dataType 'text' the success function runs, but I recieve NO data. When I send it as dataType 'json' it errors. Have no idea why.

If anyone can help, it would be greaat!

  • 写回答

1条回答 默认 最新

  • doutang7707 2018-01-25 02:49
    关注

    You don't need to JSON.stringify, you need to pass data as a JSON object:

    $(function() {
      $(".commit").on('click', function() {
        const sha_id = $(this).data("sha");
        const sha_obj = {
          "sha": sha_id
        };
        $.ajax({
          url: 'commitInfo.php',
          type: 'POST',
          data: sha_obj,
          dataType: 'json',
          success: function(response) {
            console.log(response);
          },
          error: function(resp, xhr, ajaxOptions, thrownError) {
            console.log(resp);
          }
        });
      });
    });
    

    And on commitInfo.php, you have to echo string on json format

    =====================================

    If you want to redirect to commitInfo.php you can just:

    $(".commit").on('click',function(){ 
       const sha_id = $(this).data("sha"); 
       window.location.replace("commitInfo.php?sha=" + sha_id ); 
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加