2016-01-19 19 views
0

隨處打印varialbe這裏是我的Ajax代碼:如何使用AJAX的結果,我可以在一個頁面

$("#to").change(function(){ 
    $.ajax({ 
    type: "POST", 
    url: "<?php echo base_url();?>/index.php/sales/getPrice",    
    dataType: "html",  
    data: {'from' : $('#from').val() , to: $('#to').val()},  
    success: function(response){     
    //$(".location").html(response); 
} 
});    
}); 

我想使用AJAX的結果,而不是40(下面的代碼附後)。任何想法?它不清楚請問我。

e: { 
price : 40, 
category: 'Economy' 
} 
+0

的'E:...'東西是你的'getPrice'調用返回?那是什麼?它不是json,這意味着你必須自己解析文本。但是,一旦你解析了它,你就可以直接從解析結果中提取價格值。 –

回答

-1
var result = ''; 

$("#to").change(function(){ 
    $.ajax({ 
    type: "POST", 
    async: false, 
    url: "<?php echo base_url();?>/index.php/sales/getPrice",    
    dataType: "html",  
    data: {'from' : $('#from').val() , to: $('#to').val()},  
    success: function(response){     
     result = response; 
} 
}); 
}); 

現在使用你想要這種結果:

e: { 
    price : result, 
    category: 'Economy' 
    } 
+0

只有在初始化「e」之前調用了** async **成功函數,這纔會起作用。 – Marc

+0

編輯了我的答案。 –

+0

如何使用php $ _POST ['result']; –

0

您想使用ajax調用返回的數據嗎?所以,你可以在success部分使用它:

$("#to").change(function(){ 
    $.ajax({ 
    type: "POST", 
    url: "<?php echo base_url();?>/index.php/sales/getPrice",    
    dataType: "html",  
    data: {'from' : $('#from').val() , to: $('#to').val()},  
    success: function(response){     
    alert(respone.price); 
    console.log(response); 
} 
});    
}); 

使用console.log(response);在成功的功能,讓你的瀏覽器的開發者控制檯中的數據。

相關問題