简体   繁体   中英

ajax passing parameter to php file not working

UPDATE: the php and javascript was updated/changed, based on the intial user comments:

This javascript does not get the response... what to change? How to make the javascript get the success or failed response from the php?

       jQuery('.btndel').on('click', function(e){
          e.preventDefault();
          alert("click");
          jQuery.ajax({
            url: '/file.php',
            type:'POST',
            dataType: 'json',
            data: { p: 'test' },
            success: function( data ) {
                response = JSON.parse(data);
                if (response['success']){
                     alert("success");
                } else {
                     alert("fail");
                }
            },
        });
    });

file.php

   <?php

    if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(404);
    error_log( '404 Not Found');
    exit;
   }

    header('Content-type: application/json');
    error_log("=====".$_POST['p']);
    if (!isset($_POST['p'])) {
       http_response_code(400);
       error_log( "NO PARAM");
       exit;
    }

   $response = array();
   if ($update_success === false) {
       $response['status'] = 'error';
   } else {
       $response['status'] = 'success';
   }

    exit(json_encode($response));
    ?>

what is wrong or missing?

jQuery(document).on("click", ".button", function(e) {
   e.preventDefault();
   jQuery.ajax({
      url: '/file.php',
      type: 'post',
      data: {
         p: "work"
      },
      success: function(){
          alert("not here");
      }
    });
});

try to pass parameters like this!

https://qiita.com/art_porokyu/items/ffb08251a83e4fe46d57

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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