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 ); 
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 angular开发过程中,想要读取模型文件,即图1的335行,会报404错误(如图2)。但我的springboot里配置了静态资源文件,如图3。且在该地址下我有模型文件如图4,请问该问题该如何解决呢?
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常
  • ¥15 Java,消息推送配置