duanfenhui5511 2017-06-05 00:31
浏览 46
已采纳

我不能通过javascript用ajax发布内容到php

I have a problem when sending post content to a php file via ajax. My intention is to send an object that contains the data that will be used in the file save.php and it has to process the response so that it appears in an alert window.

var iden = true;

var object = {

    'titulo': 'In the mountain',
    'dest': 'Single of',
    'edit': true,
    'previ': 'yes',
    'iden': iden
};

var xhr = new XMLHttpRequest();
xhr.open('POST', "save.php", false);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send( object);

if (xhr.status == 200) {
    xhr.onload = function () {
        alert(this.responseText);
    };
} else {
    alert('error');
}

The problem is that when trying to send the data I get this message in console:

Test.js: 56 [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.

Line 56 corresponds to:

Xhr.open ('POST', 'save.php', false);

If I set the async parameter to true, it jumps the else from the conditional if (xhr.status == 200) and displays the error alert.

Where could the problem be?

Also say that with jquery it works perfectly, but I would like to do it with Javascript.

With jquery:

$.ajax({
    url: "save.php",
    type: "post",   
    data: object
}).done(function(data) {
    alert(data);
});

EDIT:

console.log(xhr);

XMLHttpRequest {readyState: 1, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, onreadystatechange: function…} onabort : null onerror : null onload : null onloadend : null onloadstart : null onprogress : null onreadystatechange : function () ontimeout : null readyState : 4 response : "" responseText : "" responseType : "" responseURL : "http://localhost/save.php" responseXML : null status : 200 statusText : "OK" timeout : 0

  • 写回答

1条回答 默认 最新

  • doupi7619 2017-06-05 00:48
    关注

    Try changing this line of code: xhttp.open("POST", "save.php", false); and set the third parameter to true. And follow this example if it can work.

    var object = {
        'titulo': 'In the mountain',
        'dest': 'Single of',
        'edit': true,
        'previ': 'yes',
        'iden': iden
    };
    
    function loadDoc() {
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          document.getElementById("demo").innerHTML =
          this.responseText;
        }
      };
      xhttp.open("POST", "save.php", true);
      xhttp.send(object);
    <div id="demo">
    <h2>The XMLHttpRequest Object</h2>
    <button type="button" onclick="loadDoc()">Change Content</button>
    </div>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: