对PHP文件的XMLHttpRequest和Fetch请求不起作用Svelte



我无法使用Svelte从外部主机上的PHP文件中获取信息。

然而,奇怪的是,XMLHTTP请求在链接到web上托管的文本文件时有效。

这是我的JS代码:

<script>
let content = "";
function httpGet()
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "https://www.kayasuleyman.co.uk/form.php?email=example");
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send();
xmlhttp.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
content = this.responseText;
};
}
}

</script>

这是HTML:

<div id="demo">
<button on:click={httpGet}>Submit</button>
<p>Output: {content}</p>
</div>

我的PHP文件的输出,应该只是";例如";,不返回任何内容。我对这个问题感到困惑,使用fetch语句也不起作用。

有什么想法吗?

尝试将其添加到php文件的顶部:

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With");

最新更新