我有以下ajax:
$.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中的开发人员工具中看到。
然而,永远不会调用成功函数。
有人可以看到问题所在吗?
更新
好的就是:
我有两个ajax调用取决于type_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);
}
当我用id 9调用它时,我得到上面的json并且不调用success函数。
当我用id 4调用这个函数时,我得到以下json:
[{"id":"217","Category_id":"2","name":"Online session 1","description":"Guitar online session 1, du l\u00e6rer her de basale ting omkring en guitar","price":"0","mentor":null,"location":null,"start_date":null,"end_date":null,"is_online":"1","status_id":"1","Material_id":null,"duration":"5","is_private":"1","module_type":"4","gives_score":"1","type_name":"Test","type_color":"bg-type_default","category_name":"Forretningsforst\u00e5else","image_path":"\/site\/resources\/images\/category_icon\/forretningsfortaelse.png","color":"bg-business","is_owned":"true"},{"id":"222","Category_id":"10","name":"Online session 2","description":"Guitar online session 2. Du er nu klar til at blive testet.","price":"0","mentor":null,"location":null,"start_date":null,"end_date":null,"is_online":"1","status_id":"1","Material_id":null,"duration":"20","is_private":"1","module_type":"4","gives_score":"1","type_name":"Test","type_color":"bg-type_default","category_name":"Psykologi","image_path":"\/site\/resources\/images\/category_icon\/psykologi.png","color":"bg-phys","is_owned":"true"},{"id":"231","Category_id":"1","name":"Basisviden 1","description":"Basisviden Test 1 beskrivelse","price":"0","mentor":null,"location":null,"start_date":null,"end_date":null,"is_online":"1","status_id":"1","Material_id":null,"duration":"15","is_private":"1","module_type":"4","gives_score":"1","type_name":"Test","type_color":"bg-type_default","category_name":"Adf\u00e6rd","image_path":"\/site\/resources\/images\/category_icon\/adfard.png","color":"bg-adfard","is_owned":"true"},{"id":"262","Category_id":"7","name":"Ledelsesstile","description":"Med udgangspunkt i Kurt Lewins tre overordnede ledelsesformer, s\u00e5 skal du nu tage stilling til f\u00f8lgende\nudsagn og v\u00e6lge hvilken ledelsesform som passer til udsagnet.","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":"4","gives_score":"1","type_name":"Test","type_color":"bg-type_default","category_name":"Ledelse","image_path":"\/site\/resources\/images\/category_icon\/ledelse.png","color":"bg-Ledelse ","is_owned":"true"},{"id":"263","Category_id":"4","name":"Kender du typen (DISC)","description":"Vi har nu gennemg\u00e5et disc-terminologien og de forskellige adf\u00e6rdstyper,\ns\u00e5 alle er blevet pr\u00e6senteret for adf\u00e6rden bag de fire typer. Derfor er der\nher en lille stikpr\u00f8ve.","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":"4","gives_score":"1","type_name":"Test","type_color":"bg-type_default","category_name":"Kommunikation","image_path":"\/site\/resources\/images\/category_icon\/kommunication.png","color":"bg-Kommunikation ","is_owned":"true"},{"id":"264","Category_id":"4","name":"Min profil (DISC)","description":"\u00c9n ting er at kunne identificere menneskers adf\u00e6rdsprofil - men kender du din egen?","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":"4","gives_score":"1","type_name":"Test","type_color":"bg-type_default","category_name":"Kommunikation","image_path":"\/site\/resources\/images\/category_icon\/kommunication.png","color":"bg-Kommunikation ","is_owned":"true"},{"id":"281","Category_id":"11","name":"Sp\u00f8rgeteknik","description":"What, why, how er en sp\u00f8rgeteknik. \n\nAt stille et sp\u00f8rgsm\u00e5l er ikke et m\u00e5l i sig selv, kun et middel til at n\u00e5 m\u00e5let.","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":"4","gives_score":"1","type_name":"Test","type_color":"bg-type_default","category_name":"Salg & Performance","image_path":"\/site\/resources\/images\/category_icon\/salgogperformance.png","color":"bg-salg ","is_owned":"true"},{"id":"287","Category_id":"3","name":"Pol - test","description":"Dette er en test om Politiken","price":"0","mentor":null,"location":null,"start_date":null,"end_date":null,"is_online":"1","status_id":"1","Material_id":null,"duration":"5","is_private":"1","module_type":"4","gives_score":"1","type_name":"Test","type_color":"bg-type_default","category_name":"Internt","image_path":"\/site\/resources\/images\/category_icon\/internt.png","color":"bg-Internt","is_owned":"true"},{"id":"290","Category_id":"3","name":"Entercard Test","description":"Test din viden om Entercard","price":"0","mentor":null,"location":null,"start_date":null,"end_date":null,"is_online":"1","status_id":"1","Material_id":null,"duration":"5","is_private":"1","module_type":"4","gives_score":"1","type_name":"Test","type_color":"bg-type_default","category_name":"Internt","image_path":"\/site\/resources\/images\/category_icon\/internt.png","color":"bg-Internt","is_owned":"true"},{"id":"298","Category_id":"9","name":"Boconcept intro","description":"Dette er en beskrivelse","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":"4","gives_score":"1","type_name":"Test","type_color":"bg-type_default","category_name":"Produkter","image_path":"\/site\/resources\/images\/category_icon\/produkter.png","color":"bg-Produkter","is_owned":"true"},{"id":"302","Category_id":"6","name":"Test Sitel","description":"Tester test ","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":"4","gives_score":"1","type_name":"Test","type_color":"bg-type_default","category_name":"Kultur","image_path":"\/site\/resources\/images\/category_icon\/kultur.png","color":"bg-Kultur","is_owned":"true"},{"id":"304","Category_id":"1","name":"tester video","description":"1234","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":"4","gives_score":"1","type_name":"Test","type_color":"bg-type_default","category_name":"Adf\u00e6rd","image_path":"\/site\/resources\/images\/category_icon\/adfard.png","color":"bg-adfard","is_owned":"true"},{"id":"305","Category_id":"1","name":"Testing video 2","description":"1234","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":"4","gives_score":"1","type_name":"Test","type_color":"bg-type_default","category_name":"Adf\u00e6rd","image_path":"\/site\/resources\/images\/category_icon\/adfard.png","color":"bg-adfard","is_owned":"true"},{"id":"306","Category_id":"1","name":"Video vs lyd","description":"1234","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":"4","gives_score":"1","type_name":"Test","type_color":"bg-type_default","category_name":"Adf\u00e6rd","image_path":"\/site\/resources\/images\/category_icon\/adfard.png","color":"bg-adfard","is_owned":"true"},{"id":"310","Category_id":"1","name":"\u00f8\u00e5\u00e6","description":"213","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":"4","gives_score":"1","type_name":"Test","type_color":"bg-type_default","category_name":"Adf\u00e6rd","image_path":"\/site\/resources\/images\/category_icon\/adfard.png","color":"bg-adfard","is_owned":"true"},{"id":"312","Category_id":"9","name":"Produkt gennemgang","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":"4","gives_score":"1","type_name":"Test","type_color":"bg-type_default","category_name":"Produkter","image_path":"\/site\/resources\/images\/category_icon\/produkter.png","color":"bg-Produkter","is_owned":"true"},{"id":"313","Category_id":"9","name":"Produkt gennemgang","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":"4","gives_score":"1","type_name":"Test","type_color":"bg-type_default","category_name":"Produkter","image_path":"\/site\/resources\/images\/category_icon\/produkter.png","color":"bg-Produkter","is_owned":"true"}]
显示成功功能
答案 0 :(得分:1)
尝试在你的ajax代码上使用async:false,默认情况下async为true,这是它的例子
$。AJAX({
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);
})
答案 1 :(得分:0)
我怀疑你会在i
函数之后使用success
的值。但由于请求是异步的并且您同步读取i
的值,因此您可能无法获得最终值,因为JavaScript在AJAX调用之前执行命令。
可能你可以做到这一点。您可以使用alert()
而不是console.log()
来检查函数是否正在执行。这是正确的方法。
我的建议是将AJAX调用后使用i
值的代码放入函数中,并在AJAX调用的success
函数中调用该函数。
答案 2 :(得分:0)
我猜代码没有问题,你只需提醒/ console.log里面的数据成功,请看下面的示例,
$.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);
})