我编写了一个非常简单的 javascript 方法,它应该向服务器发送一个 ajax 请求,最后应该执行一个回调函数。问题是给定的回调没有被执行。我还在 chrome 开发人员工具中看到请求被执行,并返回所需的 JSON。我还将内容类型设置为 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