MySQL的jQuery Ajax调用 [英] jquery ajax call of mysql
问题描述
我发现了很多与此问题相关的主题,但不幸的是我无法使其运行.问题是我对JQuery不太了解.
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.
我试图使用JQuery进行Ajax调用,以便从mysql数据库中获取多个记录.我具有以下功能:
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 />");
}
}
});
};
我的api.php正在执行类似以下内容的mysql查询:
My api.php is executing a mysql query with something like this:
$array = retrieveUsersInfo('%'); //fetch result
echo json_encode($array);
我的主要问题是如何调试这样的问题?由于ajax正在异步调用另一个文件,因此我无法查看任何错误.从我的firefox调试器中,可以看到已输入$ .ajax函数,但未成功.
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.
谢谢.
推荐答案
可以尝试一些方法.
- 直接在浏览器中(而不是通过ajax)点击api网址,并确保它返回有效的响应.
- 向您的jquery ajax调用添加错误:function(err){}.如果服务器返回的响应不是200,则将调用此方法.
- 我使用Chrome的开发人员工具多于firefox/firebug.它具有网络"选项卡,向我显示了客户端和服务器之间的所有通信.您应该会在该标签中看到对您的api的调用.
就在手边,我认为您需要确保将MIME类型在您的php文件中设置为text/json.
just off hand, i think you need to make sure the mime-type is set to text/json in your php file.
这篇关于MySQL的jQuery Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!