Pim Van Vlaenderen
Pim Van Vlaenderen

Reputation: 117

How to retrieve data with Ajax call

i like to retrieve a string with an Ajax call but i keep getting the whole html page in my response.

What is the way to just retrieve the string?

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

Upvotes: 2

Views: 434

Answers (3)

mstrthealias
mstrthealias

Reputation: 2921

Typically a good idea to exit right after printing JSON to prevent content (maybe \n) from breaking the response.

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

Upvotes: 1

Harish Ambady
Harish Ambady

Reputation: 13121

I think your function send response with layout enabled mode, so that string comes with wrapped layout. May be you need to disable layout in the controller for the calling function (url: '{$domainpath{$language}/reservations/updatestartdates').

Upvotes: 0

Prasanth Bendra
Prasanth Bendra

Reputation: 32710

It will retrieve all the output from url: '{$domainpath}{$language}/reservations/updatestartdates',

So if you want string then only echo string in your server page(Remove all html output)

Also Change echo json_encode('only this string'); to echo json_encode(array('only this string'));

Upvotes: 2

Related Questions