简体   繁体   中英

JQuery ajax post callback function not executed

I have written a quite simple javascript method, which should send an ajax request to a server, and should finally execute a callback function. The problem is that the given callback isn't executed. I also see in the chrome developer tools that the request is executed, and returns the wished JSON. I have also set the Content type to application/json.

Here is the code I have used:

this.checkAvailability = function(){
    console.log('Before test');
    $.post('/Roomanizer-Website/Reservation', {
        action: 'checkavailability',
        categoryname: $('#categoryname').val(),
        roomcount: $('#roomcount').val(),
        start: $('#start').val(),
        end: $('#end').val()
    }, function(data){
        console.log('callback');
        console.log(data);
        if(data.available == 'true'){
            $('#start_confirmation').html(data.arrival);
            $('#end_confirmation').html(data.leaving);
            $('#price_confirmation').html(data.price);
            this.nextStep();
        } else {
            alert('There are no free rooms during your chosen period.');
        }
    }, 'json');
};

The 'Before Test' is written on the console, but not 'callback'. But as I have already said, the request is executed, and returns the correct JSON:

{arrival : '20/06/2012', leaving : '28/06/2012', price: '0.0', available: 'true'}

And last i will show the http-header:

Request URL:http://localhost:8080/Roomanizer-Website/Reservation
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Length:97
Content-Type:application/x-www-form-urlencoded
Cookie:JSESSIONID=AF1302ECA526216D8207F365F6802814
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/Roomanizer-Website/Controller?action=booking
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5
X-Requested-With:XMLHttpRequest
Form Dataview URL encoded
action:checkavailability
categoryname:Economy
roomcount:1
start:20/06/2012
end:28/06/2012
Response Headersview source
Content-Length:81
Content-Type:application/json;charset=ISO-8859-1
Date:Mon, 11 Jun 2012 12:19:45 GMT
Server:Apache-Coyote/1.1

Does anybody know hot to solve this issue?

PS: I am using JQuery 1.5.2

解决了问题,因为JSON-Parser似乎不接受单引号,所以我将返回的字符串更改为

{"arrival":"20/06/2012","leaving":"27/06/2012","price":"80.0", "available":"true"}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM