发出 Ajax POST 请求时出现“parseerror”

Posted

技术标签:

【中文标题】发出 Ajax POST 请求时出现“parseerror”【英文标题】:"parseerror" while Making an Ajax POST request 【发布时间】:2021-07-30 18:31:54 【问题描述】:

我正在尝试在 nodeJs 中使用 Ajax 发出 POST 请求。当我运行函数alert(status) 时会弹出说parseerror。这个 Ajax 调用有什么问题?

function ajaxmagic() 
$.ajax(
    url : 'FetchSongs.php',
    type : 'POST',
    data : username: "maxolgin",
    dataType: "json",
    success : function (result) 

    var i;

    for (i = 0; i < result.length; i++) 

    var table = document.getElementById("myMixTable");
    table.style.borderCollapse = "seperate"

    var rowCount = table.rows.length;
    console.log(table.rows.length)
    var row = table.insertRow(0);
    var row2 = table.insertRow(1);


    var cell1 = row.insertCell(0);
    cell1.rowSpan = "2"
    cell1.setAttribute("class", "MixImage")


    var image = document.createElement("img");
    image.style.padding = "0px 0px 0px 0px"
    cell1.appendChild(image)

    var cell2 = row.insertCell(1);
    cell2.style.width = "300px"

    var title = document.createElement("td");

    title.innerhtml = result[i].songTitle
    cell2.appendChild(title);

    var artistCell = row2.insertCell(0);

    var artistTitle = document.createElement("td");
    artistTitle.style.fontSize = "13px"
    artistCell.appendChild(artistTitle);

    var timestampCell = row.insertCell(2);
    timestampCell.rowSpan = "2"
    timestampCell.style.horizAlign = "right"


    var timestamp = document.createElement("td");
    timestamp.style.horizAlign = "right"
    timestampCell.appendChild(timestamp);

    

    ,
    error : function (jqxhr, status, exception) 
        alert(status);
    
    );


【问题讨论】:

检查网络选项卡和您的后端日志以了解请求失败的原因 你在 node.js 中做这个? @law 很确定。这是一个 .js 文件 【参考方案1】:

无论您使用的是 nodejS,还是前端反应,您都应该使用绝对路径,因为我怀疑是否有人在同一主机和端口上运行 nodejs 和 php

FetchSongs.php 应改为http://hostname:port/FetchSongs.php

【讨论】:

以上是关于发出 Ajax POST 请求时出现“parseerror”的主要内容,如果未能解决你的问题,请参考以下文章