jquery ajax调用mysql
I have found many threads regarding this issue, but unfortunately I couldn't get it running. Problem is I don't know much about JQuery.
I am trying to make an Ajax call using JQuery in order to fetch multiple records from a mysql database. I have the following function :
function updateWebpage ()
{
$.ajax({
url: './sale/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
success: function(rows) //on recieve of reply
{
for (var i in rows)
{
var row = rows[i];
var username = row[0];
var stateId = row[1];
$('#output').append("<b>id: </b>"+username+"<b> stateId: </b>"+stateId)
.append("<hr />");
}
}
});
};
My api.php is executing a mysql query with something like this:
$array = retrieveUsersInfo('%'); //fetch result
echo json_encode($array);
My main issue, is how to debug an issue like this? Since ajax is calling asynchronously another file, I cannot view any errors. From my firefox debugger, I can see that the $.ajax function is entered, but success is not.
Thanks in advance.
我找到了很多关于这个问题的线索,但不幸的是我无法让它运行。 问题是我对JQuery了解不多。 p>
我正在尝试使用JQuery进行Ajax调用,以便从mysql数据库中获取多个记录。 我有以下功能: p>
function updateWebpage()
{
$ .ajax({
url:'./sale/api.php',// 要调用以获取数据的脚本
data:“”,//您可以在此处插入url argumnets以传递给api.php
//例如“id = 5&amp; parent = 6”
dataType:'json',/ / data format
success:function(rows)//收到回复
{
for(var i in rows)
{
var row = rows [i];
var username = row [0];
var stateId = row [1];
$('#output')。append(“&lt; b&gt; id:&lt; / b&gt;”+ username +“&lt; b&gt; stateId:&lt; / b&gt;“+ stateId)
.append(”&lt; hr /&gt;“);
}
}
});
code> pre>
}; p>
我的api.php正在执行一个类似这样的mysql查询: p>
$ array = retrieveUsersInfo('%'); //获取结果
echo json_encode($ array);
code> pre>
我的主要问题是如何调试这样的问题? 由于ajax异步调用另一个文件,我无法查看任何错误。 从我的firefox调试器中,我可以看到输入了$ .ajax函数,但是没有成功。 p>
提前致谢。 p>
div>
a couple things to try.
- hit the api url directly in a browser (not through ajax) and make sure it returns the valid response.
- add an error: function(err){} to your jquery ajax call. this method will get called if there is something other than a 200 response back from the server.
- I use Chrome's developer tools more than firefox/firebug. It has a Network tab in it that shows me all the communication between the client and the server. You should see a call out to your api in that tab.
just off hand, i think you need to make sure the mime-type is set to text/json in your php file.