发出 Ajax POST 请求时出现“解析错误”

时间:2021-05-08 20:55:05

标签: html node.js ajax

我正在尝试在 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);
    }
    });

}

1 个答案:

答案 0 :(得分:0)

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

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