Submitting JSON data via JQuery ajax.post to PHP

前端 未结 4 1362
慢半拍i
慢半拍i 2020-11-28 15:33

Im submitting Data to a php file via AJAX using POST. It worked fine with just submitting strings, but now I wanted to submit my JS Object with JSON and decode it on PHP sid

4条回答
  •  -上瘾入骨i
    2020-11-28 16:03

    try this

      var vThis = this;
      this.getAbsence = function()
      {
        alert(JSON.stringify(vThis));
        jQuery.ajax({
           type: "POST",
           contentType: "application/json; charset=utf-8",
           url: "ajax/selectSingle.php?m=getAbsence",
           data: JSON.stringify(vThis),
           success : function(data){
             alert(data);
           } 
         });
       }
    

    EDIT

    I think we can also do this!

      var vThis = this;
      this.getAbsence = function()
      {
        alert(JSON.stringify(vThis));
        jQuery.ajax({
           type: "POST",
           dataType: "json",
           url: "ajax/selectSingle.php?m=getAbsence",
           data: vThis,
           success : function(data){
             alert(data);
           } 
         });
       }
    

    and in PHP

    print_r($_POST);
    

提交回复
热议问题