2

我喜欢使用 Ajax 调用检索字符串,但我在响应中不断获取整个 html 页面。

检索字符串的方法是什么?

$.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');
}
4

3 回答 3

2

它将检索所有输出url: '{$domainpath}{$language}/reservations/updatestartdates'

所以如果你想要字符串,那么echo你的服务器页面中只有字符串(删除所有 html 输出)

echo json_encode('only this string');改为echo json_encode(array('only this string'));

于 2013-02-26T08:38:46.980 回答
1

通常在打印 JSON 后立即退出以防止内容(可能是 \n)破坏响应是一个好主意。

echo json_encode('only this string');
exit();
于 2013-02-26T08:48:32.093 回答
0

我认为您的函数以启用布局模式发送响应,因此该字符串带有包装布局。可能您需要在控制器中为调用函数禁用布局 (url: '{$domainpath{$language}/reservations/updatestartdates')

于 2013-02-26T08:41:57.847 回答