Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 阻止跨源请求:使用jqgrid时_Php - Fatal编程技术网

Php 阻止跨源请求:使用jqgrid时

Php 阻止跨源请求:使用jqgrid时,php,Php,我第一次尝试使用jqgrid设置页面。我使用的是我发现的一个简单示例: index.php <?php header("Access-Control-Allow-Origin: *"); ?> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" c

我第一次尝试使用jqgrid设置页面。我使用的是我发现的一个简单示例: index.php

<?php
 header("Access-Control-Allow-Origin: *");
?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>My First Grid</title>

<link rel="stylesheet" type="text/css" media="screen" href="css/jquery-ui.css" />
<link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" />

<script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
<script src="js/grid.locale-en.js" type="text/javascript"></script>
<script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>

<script type="text/javascript">

$(function () {
   $(document).ready(function(){
    $("#list").jqGrid({
        url: 'http://localhost/jqgrid/example.php',
        datatype: "json",
        mtype: "GET",
        colNames: ["First Name", "Last Name", "Equipment Borrowed", "Service Tag", "Reason Borrowed", "Date Taken", "Expected Return Date", "Comments", "ID"],
        colModel: [
            { name: "FName",sortable: false },
            { name: "LName"},
            { name: "Eqpmnt_Brwd", align: "center"},
            { name: "Service_Tag", align: "right",sortable: false },
            { name: "Brwd_Rsn", align: "right", sortable: false },
            { name: "Date_Taken"},
            { name: "Exp_Return"},
            { name: "Comments", sortable: false },
            { name: "id"}
        ],
        pager: "#pager",
        rowNum: 5,
        autowidth: true,
        rownumbers: false,
        rowList: [5, 10, 15],
        sortname: "invid",
        sortorder: "desc",
        viewrecords: true, 
        gridview: true,
        autoencode: true,
        height: 'auto',
        width: '500',
        loadonce: true,
        caption: "Alan's Grid"
    }); 
 }); 
});
</script>

</head>
<body>
    <table id="list"><tr><td></td></tr></table> 
    <div id="pager"></div> 
</body>
</html>
在阅读CORS时,我将Apache开发服务器设置为a2enmod头,但仍然收到错误消息。我还尝试了php命令头(等等),但也没有什么效果。如果可能的话,我更喜欢客户端解决方案。我也知道我应该使用PDO,但我找不到一个例子。甚至jqgrid站点示例也不使用PDO。关于解决此问题的任何建议。

原点的定义 如果两个页面的协议、端口(如果指定了一个)和主机相同,则两个页面具有相同的来源

成功
成功
不同协议的失败 不同端口的故障 不同主机的故障

您可以使用更改域

document.domain = "yourdomain.com";

比启用跨原点访问更简单、更安全

您需要放入
标题(“访问控制允许原点:https://datasource.com");在服务器中,而不是在客户端中。
例子
这就是jqgrid的所在
是您想要获取数据的地方。
datasource中的php文件将以

<?php    
header("Access-Control-Allow-Origin: https://origin.com");
header('Access-Control-Allow-Credentials: true');

谢谢,我改为url:'',不再发出警告。然而,除了我的索引页上的列标题,我仍然没有得到任何其他信息。你有什么建议吗?你能告诉我这个请求得到了什么样的响应吗?我在example.php页面上遇到了这个错误:无法执行查询;检查与您的MySQL服务器版本对应的手册,以了解第1行“”附近要使用的正确语法。显然,MySQL在这一行$SQL=“SELECT*FROM INTORY ORDER BY$sidx$sord LIMIT$start,$LIMIT”;但是我看不出来。尝试通过放置常量而不是变量来运行此查询,如果它运行,则需要正确设置此行的格式。使用点运算符连接变量。
document.domain = "yourdomain.com";
<?php    
header("Access-Control-Allow-Origin: https://origin.com");
header('Access-Control-Allow-Credentials: true');