我正在尝试使用fetch将POST数据发送到将运行PHP脚本的远程服务器。当我尝试使用formData进行提取时,它可以工作。但是,当我将API作为第二个post变量添加到POST中时,它会抛出错误SyntaxError: Unexpected token a in JSON at position 0
。我在想,因为API实际上不是formdata,我不允许将其作为formdata附加吗?另外,我们能够在BODY中发布两种类型,让我们说formdata和text?
function exeCtCoreAPI(api) {
var url = "executetest.php";
console.log ("this is the URL: " + url); // this is correct
var formData = new FormData();
formData.append('gateway', gatewayName);
formData.append('api', api);
console.log ("these are the post data: " + api + " " + gatewayName); //these are correct
fetch(url,
{
method: "POST",
body: formData
})
.then(handleErrors)
.then(response => response.json())
.then(json => {
//do something with the json
console.log("This is the returned params: " + JSON.stringify(json));
})
.catch(error => console.log(error));
}
exeCtCoreAPI("myAPI");
它似乎在错误到达php脚本之前抛出错误,因为当我在PHP中转储POST时我没有看到任何回声。
不确定我在这里做错了什么......