gpt4 book ai didi

php - 错误 : “Origin null is not allowed by Access-Control-Allow-Origin” when using dataType text/html accessing a php file using JQuery's ajax method

转载 作者:行者123 更新时间:2023-11-30 18:32:01 25 4
gpt4 key购买 nike

为什么我在使用 dataType 文本和 html 时在 chrome 上收到此错误?如果我回显一个 javascript 并将 dataType 设置为脚本,它会起作用吗?我如何从 php 文件回显非 javascript 数据?

    client side:

$.ajax({
url: "http://localhost:8888/test.php",
type: "GET",
dataType: "text",
success: function (data) {
alert(data);
}

php file (test.php):

<?php
echo "Hello";
?>php file (test.php):

最佳答案

您有一个跨域请求问题(很可能是因为您的客户端脚本不是从 http://localhost:8888/ 提供的)。

通过设置dataTypescript您正在通过注入(inject) <script/> 启用一种处理跨域请求的请求标记到页面 DOM 中。实现它的代码是 on GitHub here .

您需要:

  • 解决 cross-domain request通过从与 PHP 相同的主机和端口提供客户端脚本来解决问题。
  • 更改脚本以返回 JSONP .

关于php - 错误 : “Origin null is not allowed by Access-Control-Allow-Origin” when using dataType text/html accessing a php file using JQuery's ajax method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9403876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com