ajax response get returned, but sometimes doesn't trigger any actions or alerts
I've got a fairly standard ajax request, and the response is returned as an array. It works perfectly 90% of the time.
Unfortunately 10% of the time, the request gets sent, response get's returned, but doesn't get displayed, and I can't even output to an alert.
Firebug shows no errors with the response. I can open the response in another window and it is fine. I've gone through the response and don't see any obvious encoding errors. Though things like st.paul's are coming out as
st.paul\x92S.The data being returned is being retrieved from a csv file which was translated to csv from an excel file.
it always fails on the same requests. So 90% of the requests go through fine, 10% will fail with no errors, but it is always the same 10%.
I'm using rails, and the output is generated by
render ({:content_type => :js, :text => @col_data.uniq})
I get the csv like this, and strip out leading and trailing whitespace. Maybe I should be looking to strip or encode other characters?
def csv_data csv=Dataset.find(session[:dataset_id]) @csv_data = CSV.read(csv.dataset.path) @csv_data = @csv_data.each{|row| row.each {|col|col.to_s.strip!}} return @csv_data end
I'd include the output here, but it is a long array, and I've gone through it looking for nil, or character encodings, but the encodings that exist are definitely escaped.
This question was originally posted as an ajax response size error because that is the obvious difference I saw between what worked, and what didn't work, but the initial comments lead me to believe that size isn't the issue as the large responses are under 45kb.
The jquery ajax code is pretty straightforward. I've tried both with dataType: 'json', and without setting a dataType. The 'alert' is triggered when the data is returned, however, in these situations where the data is not returned, the alert is not triggered. Yet, I can see the output in firebug, so their is a non-error response.
var selected_field=jQuery('option:selected',this).text(); jQuery.ajax({ url: 'selectfilter', data: 'column_name='+selected_field, dataType: 'json', success: function(data){ alert(data.toSource()); if(data.length==0){ alert('the selected column has no data'); } var list=''; for (var i=0;i'+data[i]+''; } jQuery('table#sample_data').html(list).data('list',data); } 开发者_如何学C })
Have you tried rendering your data in your controller differently, like with a "render :json => @col_data.uniq"?
You mentioned that the same 10% of data is causing this problem. Do you notice any patterns in these entries? Do you have access to the data, and did you create the data?
Many times in my job, I have exported a field-updated customer database from Excel to CSV, there have been a host of extra characters that throw errors in JavaScript. st.paul\x92S.
looks like a prime candidate, as the \x92 is unicode for the typographer single quote, or in HTML ’
I would suggest trying to clean the data by changing or removing the "smart quotes". Also, look to see if there are any other extra characters in your fields. One particular annoying character that is nearly impossible to notice in Excel is a carriage return (or new-line) character. Viewing the data in a text editor might help significantly.
精彩评论