Javascript 从服务器PHP数组提取JSON数据时出现问题

Javascript 从服务器PHP数组提取JSON数据时出现问题,javascript,php,json,wordpress,Javascript,Php,Json,Wordpress,我试图从服务器上的PHP文件创建的JSON响应中提取股票价格。我在下面描述了2个测试,测试1成功地从文件中提取JSON数据,测试2。从服务器上的aPHP脚本获取JSON数据会导致错误。我在这方面已经做了很多年了,所以如果有人能帮我找到解决方案,我将不胜感激,非常感谢 我在客户机上使用Wordpress和Woody Universal Snippets,最终在工作后将代码集成到datatables脚本中 测试1。从服务器上的文本文件获取JSON数据100%如下 <script> va

我试图从服务器上的PHP文件创建的JSON响应中提取股票价格。我在下面描述了2个测试,测试1成功地从文件中提取JSON数据,测试2。从服务器上的aPHP脚本获取JSON数据会导致错误。我在这方面已经做了很多年了,所以如果有人能帮我找到解决方案,我将不胜感激,非常感谢

我在客户机上使用Wordpress和Woody Universal Snippets,最终在工作后将代码集成到datatables脚本中

测试1。从服务器上的文本文件获取JSON数据100%如下

<script>

var ourRequest = new XMLHttpRequest();
ourRequest.open("GET", "../../Editor-PHP-1.9.0/controllers/ajax_stock_holdings_json1.txt");

//ourRequest.open("GET", "../../Editor-PHP-1.9.0/controllers/ajax_stock_holdings1.php");
//ourRequest.open("GET", "https://dividendlook.co.uk/Editor-PHP-1.9.0/controllers/ajax_stock_holdings1.php");   
ourRequest.onload = function() {
console.log(ourRequest.responseText);
    var ourDataJSON = JSON.parse(ourRequest.responseText);
}
ourRequest.send();  
</script>
测试2。失败 注释行

ourRequest.open("GET", "../../Editor-PHP-1.9.0/controllers/ajax_stock_holdings_json1.txt");
取消注释行

ourRequest.open("GET", "../../Editor-PHP-1.9.0/controllers/ajax_stock_holdings1.php");

运行下面的本地客户端脚本

<script>

var ourRequest = new XMLHttpRequest();
//ourRequest.open("GET", "../../Editor-PHP-1.9.0/controllers/ajax_stock_holdings_json1.txt");

ourRequest.open("GET", "../../Editor-PHP-1.9.0/controllers/ajax_stock_holdings1.php");
//ourRequest.open("GET", "https://dividendlook.co.uk/Editor-PHP-1.9.0/controllers/ajax_stock_holdings1.php");   
ourRequest.onload = function() {
console.log(ourRequest.responseText);
    var ourDataJSON = JSON.parse(ourRequest.responseText);
}
ourRequest.send();  
</script>
控制台日志

JQMIGRATE: Migrate is installed, version 1.4.1
(index):294 Connected successfully

709 : CTY.LSE : City of London Investment Trust Plc : 405.5
[{"symbol":"CTY.LSE","price":"405.5"}]
VM2786:1 Uncaught SyntaxError: Unexpected token C in JSON at position 0
    at JSON.parse (<anonymous>)
    at XMLHttpRequest.ourRequest.onload ((index):295)
导致以下错误

控制台显示器

Access to XMLHttpRequest at 'https://dividendlook.co.uk/Editor-PHP-1.9.0/controllers/ajax_stock_holdings1.php' from origin 'https://www.dividendlook.co.uk' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
我尝试过配置HTTP Headers插件,但没有任何效果,这可能是由于配置选项的数量太多了,如果有的话,知道哪些选项可以清除我的错误

我已将代码添加到我的服务器PHP文件中,以修复访问控制允许源代码问题,如下所示:-

<?php
 /* Loads the WordPress environment and template */
require( '../../wp-blog-header.php' );

global $current_user;
wp_get_current_user();

// DataTables PHP library
include( "../lib/DataTables.php" );

// HTTP Header 

header('Access-Control-Allow-Origin: https://dividendlook.co.uk');

$json_array = array();
$stock_id = 709; // CTY.LSE

...etc as before

将以下行替换为PHP文件,而不是使用sitename替换前一行

header('Access-Control-Allow-Origin: *');

导致错误

Failed to load resource: the server responded with a status of 404 ()
jquery-migrate.min.js?ver=1.4.1:2 JQMIGRATE: Migrate is installed, version 1.4.1
(index):294 Connected successfully

709 : CTY.LSE : City of London Investment Trust Plc : 405.5
[{"symbol":"CTY.LSE","price":"405.5"}]
(index):1 Uncaught SyntaxError: Unexpected token C in JSON at position 0
    at JSON.parse (<anonymous>)
    at XMLHttpRequest.ourRequest.onload ((index):295)
generic-no-float.css:1 Failed to load resource: the server responded with a status of 404 ()
加载资源失败:服务器响应状态为404()
jquery migrate.min.js?ver=1.4.1:2 JQMIGRATE:migrate已安装,版本为1.4.1
(索引):294已成功连接
709:CTY.LSE:伦敦金融城投资信托有限公司:405.5
[{“symbol”:“CTY.LSE”,“价格”:“405.5”}]
(索引):1未捕获的语法错误:JSON中位于位置0的意外标记C
在JSON.parse()处
在XMLHttpRequest.ourRequest.onload((索引):295)
通用无浮点。css:1未能加载资源:服务器响应状态为404()
我在.htaccess的末尾添加了以下代码,该代码修复了CORS策略:“访问控制允许来源”问题,现在错误是

''' 加载资源失败:服务器响应状态为404() jquery migrate.min.js?ver=1.4.1:2 JQMIGRATE:migrate已安装,版本为1.4.1
(索引):290 ourRequest.responseText>>:[{“symbol”:“CTY.LSE”,“price”:“405.5”}]:在您的服务器上,PHP。添加那些必需的标题。浏览器拒绝来自不同来源的响应。要完成此工作,请将访问控制允许原点设置为“*”。这是来自任何来源的任何请求都会得到响应。

感谢您的快速响应,将访问控制允许来源设置为“*”的命令很有意义。在服务器PHP文件中。谢谢ColinCeoFred这段代码是你的意思的一个例子吗,我将尝试添加所需的代码CeoFred我已为呼叫添加了更多详细信息,以响应您的建议,感谢您的添加。htaccess Header添加访问控制允许来源“”Header添加访问控制允许方法:“GET、POST、OPTIONS、DELETE、PUT”我已在上面的主要问题中显示结果,谢谢Colin
Access to XMLHttpRequest at 'https://dividendlook.co.uk/Editor-PHP-1.9.0/controllers/ajax_stock_holdings1.php' from origin 'https://www.dividendlook.co.uk' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
<?php
 /* Loads the WordPress environment and template */
require( '../../wp-blog-header.php' );

global $current_user;
wp_get_current_user();

// DataTables PHP library
include( "../lib/DataTables.php" );

// HTTP Header 

header('Access-Control-Allow-Origin: https://dividendlook.co.uk');

$json_array = array();
$stock_id = 709; // CTY.LSE

...etc as before

Failed to load resource: the server responded with a status of 404 ()
jquery-migrate.min.js?ver=1.4.1:2 JQMIGRATE: Migrate is installed, version 1.4.1
(index):1 Access to XMLHttpRequest at 'https://dividendlook.co.uk/Editor-PHP-1.9.0/controllers/ajax_stock_holdings1.php' from origin 'https://www.dividendlook.co.uk' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value 'https://divdendook.co.uk' that is not equal to the supplied origin.
generic-no-float.css:1 Failed to load resource: the server responded with a status of 404 ()
header('Access-Control-Allow-Origin: *');

Failed to load resource: the server responded with a status of 404 ()
jquery-migrate.min.js?ver=1.4.1:2 JQMIGRATE: Migrate is installed, version 1.4.1
(index):294 Connected successfully

709 : CTY.LSE : City of London Investment Trust Plc : 405.5
[{"symbol":"CTY.LSE","price":"405.5"}]
(index):1 Uncaught SyntaxError: Unexpected token C in JSON at position 0
    at JSON.parse (<anonymous>)
    at XMLHttpRequest.ourRequest.onload ((index):295)
generic-no-float.css:1 Failed to load resource: the server responded with a status of 404 ()
<script>    
var ourRequest = new XMLHttpRequest();
ourRequest.open("GET", "https://dividendlook.co.uk/Editor-PHP-1.9.0/controllers/ajax_stock_holdings1.php"); 
ourRequest.onload = function() {
console.log("ourRequest.responseText >>>>>:"+ourRequest.responseText+":<<<<");
}
ourRequest.send();  
</script>