我一直在尝试查看一个示例,以弄清楚如何使用JQuery,AJAX和PHP从客户端连接到服务器的SQL数据库,尽管它很旧,但看起来做得很好,很简单:Example Link。单个文件夹包含我所有的php文件以及jQuery的产品版本(javascript-1.10.2.min.js)。
问题3-已修正
JS控制台在以下位置显示[Object,“ parsererror”,SyntaxError]
var id = data.data[0]; //get id, data['data'][0] works here as well
在client.php中。对象responseText显示...“未选择数据库” ...我已经基于Daedalus的响应更新了client.php,并且仍然遇到相同的错误。
错误是在server-api.php中错误标记了变量($ link而不是$ con)
- 代码 -
db-connect.php:
<?php
//--------------------------------------------------------------------------
// Example php script for fetching data from mysql database
//--------------------------------------------------------------------------
$host = "localhost";
$user = "root";
$pass = "password";
$databaseName = "server-db";
$tableName = "inventory";
?>
server-api.php:
<?php
//--------------------------------------------------------------------------
// 1) Connect to mysql database
//--------------------------------------------------------------------------
include 'db-connect-99k.php';
$con = mysql_connect($host,$user,$pass);
$db_selected = mysql_select_db('zgc7009_99k_db', $con);
$array = array('mysql' => array('errno' => mysql_errno($con), 'errtxt' =>mysql_error($con)));
//--------------------------------------------------------------------------
// 2) Query database for data
//--------------------------------------------------------------------------
$result = mysql_query("SELECT * FROM $tableName"); //query
$array['mysql'][] = array('errno' => mysql_errno($con), 'errtxt' =>mysql_error($con));
$array['data'] = mysql_fetch_row($result); //fetch result
//--------------------------------------------------------------------------
// 3) echo result as json
//--------------------------------------------------------------------------
echo json_encode($array);
?>
client.php
<html>
<head>
<script language="javascript" type="text/javascript" src="jquery-1.10.2.min.js"></script>
</head>
<body>
<!-------------------------------------------------------------------------
1) Create some html content that can be accessed by jquery
-------------------------------------------------------------------------->
<h2> Client example </h2>
<h3>Output: </h3>
<div id="output">this element will be accessed by jquery and this text replaced</div>
<script id="source" language="javascript" type="text/javascript">
$(function ()
{
//-----------------------------------------------------------------------
// 2) Send a http request with AJAX http://api.jquery.com/jQuery.ajax/
//-----------------------------------------------------------------------
$.ajax({
url: 'server-api.php', //the script to call to get data
data: "", //you can insert url argumnets here to pass to api.php
//for example "id=5&parent=6"
//dataType: 'json', //data format (comment out or get parsererror)
// Successful network connection
// Successful network connection
success: function(data) //on recieve of reply
{
var id = data.data[0]; //get id, data['data'][0] works here as well
var vname = data.data[1]; //get name
//--------------------------------------------------------------------
// 3) Update html content
//--------------------------------------------------------------------
$('#output').html("<b>id: </b>"+id+"<b> name: </b>"+vname); //Set output element html
$('#error_code').html("Success!");
},
error: function() {
console.log(arguments);
}
});
});
</script>
</body>
</html>
问题1-已修复
多亏了用户的帮助,我终于摆脱了原来的错误:
OPTIONS file:///C:/Users/zgc7009/Desktop/Code/Web/php/server-api.php No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. jquery.js:8706
XMLHttpRequest cannot load file:///C:/Users/zgc7009/Desktop/Code/Web/php/server-api.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
问题2-已修复[现在正在临时Web服务器上运行(请参阅底部的链接)]
现在,我正在将WAMP(包括phpmyadmin和apache)作为我的Web服务器。我可以使用脚本(client.php)来运行我的php页面,它可以运行,似乎无法在我的日志中找到任何错误。但是,我似乎仍然从未遇到过脚本的成功功能。我假设我在某处(例如localhost /“我的网站” .php)进行了不适当的设置,但是我不确定在哪里。
我还尝试过一些更改我的AJAX函数,以包括.done:
$.ajax({
url: 'localhost/server-api.php', //the script to call to get data
data: "", //you can insert url argumnets here to pass to api.php
//for example "id=5&parent=6"
dataType: 'json', //data format
// Successful network connection
success: function(data) //on recieve of reply
{
var id = data[0]; //get id
var vname = data[1]; //get name
//--------------------------------------------------------------------
// 3) Update html content
//--------------------------------------------------------------------
$('#output').html("<b>id: </b>"+id+"<b> name: </b>"+vname); //Set output element html
}
}).done(function() {
$('#output').html("AJAX complete");
});
但是我的输出值在ajax调用中从未改变。我可能错误地实现了.done,但是我似乎无法弄清为什么我什么都没击中,并且似乎找不到可以帮助下一步的日志。
在以前的编辑中,我从php调用中删除了localhost(“ localhost / server-api.php”返回了404),现在又被卡住了。我从jQuery调用中收到304 Not Modified,但我认为,从jQuery 1.5开始,ajax成功地实现了这一点,因此我仍然应该实现html文本更新(正确吗?),但我没有。
WAMP访问日志:
127.0.0.1 - - [14/Jan/2014:14:22:45 -0500] "GET /client.php HTTP/1.1" 200 2146
127.0.0.1 - - [14/Jan/2014:14:22:45 -0500] "GET /jquery.js HTTP/1.1" 304 -
127.0.0.1 - - [14/Jan/2014:14:22:45 -0500] "GET /server-api.php HTTP/1.1" 200 38
注意-这是我在浏览器中刷新client.php时唯一更新的日志。我的js控制台保持空白。我已将其上传到临时站点:zgc7009.99k.org/client-99k.php
如果提出以下内容,请原谅我,但我想尽我所能解释。
首先,如注释中所述error
,.ajax()
仅当在尝试加载必要的php页面时(或您未加载的php页面时出错),才会调用jQuery方法的方法(如果未指定url,则使用当前页面))已指定。在这方面的错误可能是404(找不到页面),500(服务器错误)或您拥有的东西。
您遇到的当前错误有两个方面:
localhost/path/to/file.extension)
关于问题#1,需要由您的PHP解释器处理一个php页面,您需要在系统上安装该php解释器。我建议使用xampp之类的东西来适应这种情况,尽管还有很多其他选择。
当访问该计算机上运行的服务器,一个使用localhost
在地址栏中的URL,没有协议(http://
,https://
,ftp://
等),也从不file:///
协议。例如,如果我要访问文件夹测试的index.php文件,它将为localhost/test/index.php
。
关于问题#2,浏览器具有各种限制,以防止执行恶意代码。这些限制之一是Same Origin policy,该策略限制来源与原始请求不同的文档接受该请求。例如..
如果我们在处有一个服务器domain.website.com
,并且向发出请求otherdomain.website.com
,则该请求将失败,因为请求的端点位于另一个域上。
同样,相同的存在对于制造方面的任何请求file:///
的协议。它始终是1的产地不同对待,它总是会1失败。可以更改此行为,但不建议这样做,因为它是一个安全漏洞。
当然,要解决所有这些问题..在您的机器上安装Web服务器(例如xampp或wamp)(取决于您的操作系统)或使用托管的Web服务器,请不要通过双击打开html文件,而只能通过访问其url来打开它。 (根据您的网络服务器的目录(每个服务器的目录不同)),并始终确保您的域和端口匹配。
编辑1:
不知道为什么我以前没有看到这个;我们本可以避免头痛的。.无论如何,首先,更改错误并在此处执行以下操作:
$dbs = mysql_select_db($databaseName, $con);
echo mysql_errno($con) . ": " . mysql_error($con). "\n";
到:
$array = array('mysql' => array('errno' => mysql_errno($con), 'errtxt' =>mysql_error($con)));
然后,将数据库处理后的数组集更改为此:
$result = mysql_query("SELECT * FROM $tableName"); //query
$array['mysql'][] = array('errno' => mysql_errno($con), 'errtxt' =>mysql_error($con));
$array['data'] = mysql_fetch_row($result);
解释我已更改的内容以及原因。.您的第一个回显是在解析回显的json时导致json解析器失败。如果您在刷新过程中未打开控制台,则不会看到它确实执行了ajax请求。您也没有定义错误处理程序,因此您永远不会知道。为了解析我上面刚刚创建的新json,请将上面的成功处理程序的变量声明修改为:
var id = data.data[0]; //get id, data['data'][0] works here as well
var vname = data.data[1]; //get name
当然,如果您的mysql引起任何错误,则可以使用以下命令访问这些错误:
console.log(data.mysql);
同样,在您的成功职能中。要查看您的实际.ajax()
方法是否有错误,可以为错误处理程序执行以下操作:
error: function() {
console.log(arguments);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句