jQuery AJAX/POST not sending data to PHP

前端 未结 7 1362
春和景丽
春和景丽 2020-12-29 03:54

So I have this problem for a while now, and I know there are countless questions on this topic, believe me I tried every solution possible but still does not work.

T

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-29 04:48

    You need to set the data type as json in ajax call.

    JQUERY CODE:

    $.ajax({
      url: "ajax/add-user.php",
      type: "POST",
      dataType:'json',
      data: {name: 'John'},
      success: function(data){
          console.log(data);
      }
    });
    

    At the same time verify your backend code(php), whether it can accept json data ?

    If not, configure the header as below:

    PHP CODE:

    /**
     * Send as JSON
     */
    header("Content-Type: application/json", true);
    

    Happy Coding :

提交回复
热议问题