Javascript 返回Jquery ajax数据,但未调用success函数
我有以下ajax:Javascript 返回Jquery ajax数据,但未调用success函数,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有以下ajax: $.ajax({ type: 'POST', url: '/Module/findByType', dataType: 'json', data: { request: 'ajax', type_id: id }, success: function (data) { var i = 0;
$.ajax({
type: 'POST',
url: '/Module/findByType',
dataType: 'json',
data: {
request: 'ajax',
type_id: id
},
success: function (data)
{
var i = 0;
})
这将生成以下输出:
[{"id":"267","Category_id":"1","name":"Adf\u00e6rdsscreening","description":"Denne adf\u00e6rdsscreening tager udgangspunkt i Adam Adf\u00e6rds 5 parametre, og du giver karakter ud\nfra en skala fra 1 til 4, hvor 4 er bedst.","price":"0","mentor":null,"location":null,"start_date":null,"end_date":null,"is_online":"1","status_id":"1","Material_id":null,"duration":"10","is_private":"1","module_type":"9","gives_score":"1","type_name":"Screening","type_color":"bg-Survey","category_name":"Adf\u00e6rd","image_path":"\/site\/resources\/images\/category_icon\/adfard.png","color":"bg-adfard","is_owned":"true"},{"id":"271","Category_id":"4","name":"Samtalescreening","description":"Jo mere du \u00f8ver dig p\u00e5 noget, jo dygtigere bliver du til det. M\u00e5ske har du\r\nf\u00f8r oplevet, at du g\u00f8r store fremskridt i de f\u00f8rste uger, derefter g\u00e5r det\r\nlangsommere og langsommere.\r\nOg til sidst g\u00e5r det helt i st\u00e5, m\u00e5ske endda i lange perioder.","price":"0","mentor":null,"location":null,"start_date":null,"end_date":null,"is_online":"1","status_id":"1","Material_id":null,"duration":"10","is_private":"1","module_type":"9","gives_score":"1","type_name":"Screening","type_color":"bg-Survey","category_name":"Kommunikation","image_path":"\/site\/resources\/images\/category_icon\/kommunication.png","color":"bg-Kommunikation ","is_owned":"true"},{"id":"289","Category_id":"1","name":"Test screening","description":"","price":"0","mentor":null,"location":null,"start_date":null,"end_date":null,"is_online":"1","status_id":"1","Material_id":null,"duration":"10","is_private":"1","module_type":"9","gives_score":"1","type_name":"Screening","type_color":"bg-Survey","category_name":"Adf\u00e6rd","image_path":"\/site\/resources\/images\/category_icon\/adfard.png","color":"bg-adfard","is_owned":"true"},{"id":"292","Category_id":"1","name":"Test 2","description":"","price":"0","mentor":null,"location":null,"start_date":null,"end_date":null,"is_online":"1","status_id":"1","Material_id":null,"duration":"0","is_private":"1","module_type":"9","gives_score":"1","type_name":"Screening","type_color":"bg-Survey","category_name":"Adf\u00e6rd","image_path":"\/site\/resources\/images\/category_icon\/adfard.png","color":"bg-adfard","is_owned":"true"}]
我可以从chrome的开发者工具中看到
但是,从未调用success函数
有人能看出问题出在哪里吗
更新
好吧,事情是这样的:
我的ajax调用有两个方面,具体取决于类型_id,因此下面是我非常复杂的PHP代码:
public function findByType()
{
if ($_SESSION['User']['type'] <= 2) {
if(isset($_POST['type_id']))
{
$organization_modules = $this->getDatabase()->template('
SELECT Module.*, MT.name as type_name, MT.color as type_color, C.name as category_name, C.image_path as image_path, C.color FROM module Module
INNER JOIN organization_has_module OHOM
ON Module.id = OHOM.Module_id JOIN category C ON C.id = Module.category_id INNER JOIN module_type MT on MT.id = Module.module_type
WHERE OHOM.Organization_id = ' . $_SESSION['User']['org'] . '
AND Module.module_type = ' . $_POST['type_id'].' AND Module.status_id != 0', MySqlTemplates::RFQ_FM);
$all_modules = $this->getDatabase()->template('SELECT
M . *,
MT.color as type_color,
MT.name as type_name,
C.name as category_name,
C.image_path as image_path,
C.color
FROM
module M
JOIN
category C ON C.id = M.category_id
JOIN
module_type MT ON MT.id = M.module_type
WHERE
M.module_type = '.$_POST['type_id'].' AND is_private = 0 AND M.status_id != 0', MySqlTemplates::RFQ_FM);
}
else
{
$organization_modules = $this->getDatabase()->template('
SELECT Module.*, MT.name as type_name, MT.color as type_color, C.name as category_name, C.image_path as image_path, C.color FROM module Module
INNER JOIN organization_has_module OHOM
ON Module.id = OHOM.Module_id JOIN category C ON C.id = Module.category_id INNER JOIN module_type MT on MT.id = Module.module_type
WHERE OHOM.Organization_id = ' . $_SESSION['User']['org'] . '
AND Module.module_type = ' . $_POST['type_id'].' AND Module.status_id != 0', MySqlTemplates::RFQ_FM);
$all_modules = $this->getDatabase()->template('SELECT
M . *,
MT.color as type_color,
MT.name as type_name,
C.name as category_name,
C.image_path as image_path,
C.color
FROM
module M
JOIN
category C ON C.id = M.category_id
JOIN
module_type MT ON MT.id = M.module_type
WHERE
M.module_type = '.$_POST['type_id'].' AND is_private = 0 AND M.status_id != 0', MySqlTemplates::RFQ_FM);
}
if($organization_modules)
{
if($all_modules == null)
{
$all_modules = array();
}
foreach ($organization_modules as $module)
{
$is_owned = false;
$module['is_owned'] = 'true';
$module['name'] = $module['name'];
for ($i = 0, $size = count($all_modules); $i < $size; $i++)
{
$all_modules[$i]['name'] = $all_modules[$i]['name'];
if($all_modules[$i]['id'] == $module['id'])
{
$is_owned = true;
$all_modules[$i]['is_owned'] = 'true';
}
}
if(!$is_owned)
{
array_push($all_modules, $module);
}
}
}
else
{
if(isset($_POST['type_id']))
{
$all_modules = $this->getDatabase()->template('SELECT M.*, C.name as category_name, C.image_path as image_path, C.color FROM module M JOIN category C ON C.id = M.category_id JOIN module_type MT ON MT.id = M.module_type WHERE M.module_type = ' . $_POST['type_id'].' AND is_private = 0', MySqlTemplates::RFQ_FM);
}
else
{
$all_modules = $this->getDatabase()->template('SELECT M.*, C.name as category_name, C.image_path as image_path, C.color FROM module M JOIN category C ON C.id = M.category_id JOIN module_type MT ON MT.id = M.module_type WHERE M.module_type = ' . $_POST['type_id'].' AND is_private = 0', MySqlTemplates::RFQ_FM);
}
}
} else {
$all_modules = $this->getDatabase()->template('
SELECT M.*, C.name as category_name, C.image_path as image_path, C.color, MT.name as type_name, MT.color as type_color FROM module M
INNER JOIN user_has_module UHOM
ON M.id = UHOM.Module_id
JOIN
category C ON C.id = M.category_id INNER JOIN module_type MT ON M.module_type = MT.id
WHERE UHOM.user_id = ' . $_SESSION['User']['id'] . ' AND M.category_id = ' . $_POST['category_id'], MySqlTemplates::RFQ_FM);
for ($i = 0, $size = count($all_modules); $i < $size; $i++)
{
$all_modules[$i]['is_owned'] = 'true';
}
}
print json_encode($all_modules);
}
并且显示了success函数我怀疑您是否在
success
函数之后使用了I
的值。但是,由于请求是异步的,并且同步读取i
的值,因此可能无法获得最终值,因为JavaScript在AJAX调用之前执行命令
也许你能做到。您可以使用console.log()
,而不是alert()
,检查函数是否正在执行。这是正确的方法
我的建议是将使用
i
值的代码放在AJAX调用之后的函数中,并在AJAX调用的success
函数中调用该函数。我猜代码没有问题,您只需提醒/console。将数据记录在成功中,请参见下面的示例
$.ajax({
type: 'POST',
url: '/Module/findByType',
dataType: 'json',
data: {
request: 'ajax',
type_id: id
},
success: function (data)
{
var i = 0;
console.log("Value of i:" + i);
console.log("Result:" + data);
})
只要试着在ajax代码中使用async:false,默认情况下async为true,下面是它的示例 $.ajax({
你怎么说success函数从来没有被调用过?因为success函数没有任何交互功能,你不能这么说。我认为问题出在你的PHP代码中,而不是JS。你能上传它吗?@PraveenKumar我试图在success函数中发出警告你的服务器端代码可能没有以正确的格式返回数据,但是你可以上传数据看,可能还可以。@ArindamNayak我可以发布我的php代码。他说他试图在成功块中发出警报,但从未发生过。@IdanMagled更新了答案。很抱歉,混淆。从未调用成功函数。因此没有执行任何代码相同的结果没有调用任何内容。我想问题是在php中而不是在JS中。您的JS代码看起来不错。请检查PHP代码
$.ajax({
type: 'POST',
url: '/Module/findByType',
dataType: 'json',
data: {
request: 'ajax',
type_id: id
},
success: function (data)
{
var i = 0;
console.log("Value of i:" + i);
console.log("Result:" + data);
})
type: 'POST',
url: '/Module/findByType',
dataType: 'json',
async : false,
data: {
request: 'ajax',
type_id: id
},
success: function (data)
{
var i = 0;
console.log("Value of i:" + i);
console.log("Result:" + data);
})