2013-02-26 81 views
2

我喜歡用Ajax調用來檢索字符串,但我一直在我的響應中獲取整個html頁面。如何使用Ajax調用來檢索數據

什麼是剛纔檢索字符串的方式?

$.ajax({ 
        url: '{$domainpath}{$language}/reservations/updatestartdates', 
        data: {property:property,stayduration:stayduration}, 
        type: 'POST', 
        dataType: 'json' 
       }).done(function(response){ 
        alert(response); 
       }); 

private function updateAvailableStartDates(){ 
    if(isset($_POST['property']) && !empty($_POST['property']) && isset($_POST['stayduration']) && !empty($_POST['stayduration'])){ 
     $property = $_POST['property']; 
     $stayduration = $_POST['stayduration']; 
    } 
    //handle code 

    echo json_encode('only this string'); 
} 
+1

如果是AJAX請求,您需要echo _only_字符串 – 2013-02-26 08:39:12

+0

@DipeshParmar這有什麼好處? – 2013-02-26 08:41:28

+0

在輸出頁面內容之前放置你的更新函數,並在'echo'後添加一個'exit;',這樣它就會在POST請求匹配時退出等。 – adeneo 2013-02-26 08:43:05

回答

1

通常,打印JSON後立即退出是一個好主意,以防止內容(可能是\ n)打破響應。

echo json_encode('only this string'); 
exit(); 
+0

這個確實對我有用!我也必須改變我的編碼,像這樣'echo json_encode(array('success'=> $ string));'並用'availableDates = response.success.split(',');' – 2013-02-28 08:34:18

2

它會檢索所有從url: '{$domainpath}{$language}/reservations/updatestartdates'輸出,

所以,如果你想字符串,則只有echo在您的服務器網頁字符串(刪除所有HTML輸出)

也會改變echo json_encode('only this string');echo json_encode(array('only this string'));

0

我認爲你的函數發送了啓用佈局模式的響應,以便字符串帶有包裝佈局。可能是您需要禁用控制器中的調用函數 (url: '{$domainpath{$language}/reservations/updatestartdates')的佈局。