Submitting JSON data via JQuery ajax.post to PHP

前端 未结 4 1371
慢半拍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条回答
  •  青春惊慌失措
    2020-11-28 16:14

    To me, it looks like you should reformat your AJAX object. The url-property should only be the URL for the target php-file and any data that needs to be posted should be in the form of a query-string in the data-property. The following should work as you expected:

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

提交回复
热议问题