我编写了一个非常简单的javascript方法,该方法应该向服务器发送ajax请求,并最终应该执行回调函数。问题是没有执行给定的回调。我还在chrome开发人员工具中看到请求已执行,并返回所需的JSON。我还将Content类型设置为application / json。
以下是我使用的代码:
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');
};
“测试前”写在控制台上,但不是“回调”。但正如我已经说过的那样,请求被执行,并返回正确的JSON:
{arrival : '20/06/2012', leaving : '28/06/2012', price: '0.0', available: 'true'}
最后我将展示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
有人知道解决这个问题很热吗?
PS:我正在使用JQuery 1.5.2
答案 0 :(得分:0)
问题解决了,因为JSON-Parser似乎不接受单引号,所以我将返回的字符串更改为
{"arrival":"20/06/2012","leaving":"27/06/2012","price":"80.0", "available":"true"}