简体   繁体   English

jQuery ajax发布回调函数未执行

[英]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. 我已经编写了一个非常简单的javascript方法,该方法应该将ajax请求发送到服务器,并最终执行回调函数。 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. 我还在chrome开发人员工具中看到执行了请求,并返回了所希望的JSON。 I have also set the Content type to application/json. 我也将内容类型设置为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: 但是正如我已经说过的那样,该请求已执行,并返回正确的JSON:

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

And last i will show the http-header: 最后,我将显示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 PS:我正在使用JQuery 1.5.2

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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