weird behavior on many ajax posts
guys! Here is my issue. I have a select that changes the price range using jquery ajax post. The problem is, once in a while, no particular trigger, the post action fails and I get a 404 error, (after many 200 OKs).Have you encountered anything like this before? Might this be related to the code or is just a server problem? T开发者_如何学Gohis is the js function:
function changePriceRange(event_id,elem){
alert('pr'+elem.val());
var data =
{
event_id:event_id,
prange:elem.val(),
event_name:$('#event_name').val(),
event_date:$('#event_date3').val()
}
if(event_id==0){
var my_url = base_url+'change_prange_new';
}else{
var my_url = base_url+'change_prange';
}
var request =
{
url:my_url,
type:'POST',
data:data,
success:function(response)
{
$('#section3_items').html(response);
}
}
$.ajax(request);
//update gifts section
var data2 =
{
event_id:event_id,
prange:elem.val()
}
var request2 =
{
url:base_url+'update_gifts_section',
type:'POST',
data:data2,
success:function(response)
{
$('#gifts_section').html(response);
}
}
$.ajax(request2);
}
..and the 404 error is happening on update_gifts_section. Thank you..
When a server replies with code 404, it means the document corresponding to the URL of the request is not found.
In your ajax call about "Update Gifts", you specify the URL as
url:base_url+'update_gifts_section'
If base_url is a static variable (means not changing with respect to some piece of code), then the URL is always the same. If a request gets HTTP200 and another request gets HTTP404, it means the problem is based on server, not the code. (Which is basically your question).
To solve the issue, adding more details about how you are calling this method, what url is called (observe with fiddler etc.) would be helpful.
You need to find out more about the error. A 404 means the page is not found so is there a problem with base_url
? Is it sometimes null, for instance?
The best thing for you to do is to add a failure callback so that you know when the ajax call fails. This will help you figure out the problem:
var request2 =
{
url:base_url+'update_gifts_section',
type:'POST',
data:data2,
success:function(response)
{
$('#gifts_section').html(response);
}
error: function(xhr,err){
alert('failure');
alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
alert("responseText: "+xhr.responseText); {
}
}
i just update your code to look better...
function changePriceRange(event_id,elem){
alert('pr'+elem.val());
var my_url = (!event_id) ? base_url+'change_prange_new' : base_url+'change_prange';
jQuery.ajax({
type:'POST',
url:my_url,
data:({
event_id:event_id,
prange:elem.val(),
event_name:jQuery('#event_name').val(),
event_date:jQuery('#event_date3').val()
}),
success:function(response)
{
jQuery('#section3_items').html(response);
},
error: function(xhr,err){
alert('failure');
alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
alert("responseText: "+xhr.responseText);
}
});
//update gifts section
jQuery.ajax({
type:'POST',
url:base_url+'update_gifts_section',
data:({
event_id:event_id,
prange:elem.val()
}),
success:function(response){
jQuery('#gifts_section').html(response);
},
error: function(xhr,err){
alert('failure');
alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
alert("responseText: "+xhr.responseText);
},
statusCode: {
404: function() {alert('page not found');}
}
});
}
精彩评论