this is a noob question.
这是一个不能回答的问题。
my javascript function(part of knockout.js model that I have defined):
我的javascript函数。我定义的js模型):
self.loadData = function(){
alert("loadData got called");
$.ajax({
url: 'database_connection.php',
dataType: 'json',
success: function(data){ //json string of records returned from server
alert('success from server call');
},
error: function(){
alert('error from server call');
}
});
};
Contents of database_connection.php:
database_connection.php内容:
<?php
echo "this is called";
$db = new MySqli('localhost', 'username', 'password', 'database');
$activities = $db->query("SELECT * FROM MainActivity");
$activities_r = array();
while($row = $activities->fetch_array()){
$val = $row['mActivityID'];
$act = $row['Name'];
$activities_r[] = array('val'=>$val, 'act' => $act);
}
echo json_encode($activities_r);
?>
The php is correct, coz if I directly access this file through browser, it correctly displays the result from database table.
php是正确的,因为如果我直接通过浏览器访问这个文件,它会正确地显示来自数据库表的结果。
However, when executed through the loadData
function, I get two alerts: 1. "loadData is called"
2. "error from server call"
但是,当通过loadData函数执行时,我将得到两个警报:1。“loadData叫做“2。“从服务器调用错误”
the first line of database_connection.php is not being executed since I cant see the result of echo
, so that means the script is not getting called.
database_connection的第一行。php没有被执行,因为我看不到echo的结果,所以这意味着脚本没有被调用。
Am I using the ajax
function wrongly?
我是否错误地使用了ajax函数?
3 个解决方案
#1
2
Your AJAX request contains:
AJAX请求包含:
dataType: "json"
This means that if server returns invalid JSON with a 200 OK status then jQuery fires the error function
这意味着如果服务器返回无效的JSON和200 OK状态,那么jQuery将触发错误函数。
Use the following code to ensure the reponse is JSON format.. (PHP vsersion)
使用以下代码确保响应是JSON格式。(PHP vsersion)
header('Content-Type: application/json');
Note : empty response is also considered invalid JSON; you could return {} or null which validate as JSON
注意:空响应也被认为是无效的JSON;您可以返回{}或null,该值验证为JSON
#2
1
you need to add headers in php file, because your data type is json in ajax call.
您需要在php文件中添加头文件,因为在ajax调用中,您的数据类型是json。
header('Content-Type: application/json');
echo json_encode($activities_r);
#3
0
You need to set the type
of your request and may be remove dataType
. Also in the success
callback it was one extra bracket. Check it :
您需要设置请求的类型,可以删除数据类型。在成功回调中还有一个额外的括号。检查:
self.loadData = function(){
alert("loadData got called");
$.ajax({
url: 'database_connection.php',
type : 'GET',
// dataType: 'json',
sucess: function(data){ //json string of records returned from server
alert('success from server call');
},
error: function(){
alert('error from server call');
}
});
};
#1
2
Your AJAX request contains:
AJAX请求包含:
dataType: "json"
This means that if server returns invalid JSON with a 200 OK status then jQuery fires the error function
这意味着如果服务器返回无效的JSON和200 OK状态,那么jQuery将触发错误函数。
Use the following code to ensure the reponse is JSON format.. (PHP vsersion)
使用以下代码确保响应是JSON格式。(PHP vsersion)
header('Content-Type: application/json');
Note : empty response is also considered invalid JSON; you could return {} or null which validate as JSON
注意:空响应也被认为是无效的JSON;您可以返回{}或null,该值验证为JSON
#2
1
you need to add headers in php file, because your data type is json in ajax call.
您需要在php文件中添加头文件,因为在ajax调用中,您的数据类型是json。
header('Content-Type: application/json');
echo json_encode($activities_r);
#3
0
You need to set the type
of your request and may be remove dataType
. Also in the success
callback it was one extra bracket. Check it :
您需要设置请求的类型,可以删除数据类型。在成功回调中还有一个额外的括号。检查:
self.loadData = function(){
alert("loadData got called");
$.ajax({
url: 'database_connection.php',
type : 'GET',
// dataType: 'json',
sucess: function(data){ //json string of records returned from server
alert('success from server call');
},
error: function(){
alert('error from server call');
}
});
};