Data not being POSTed through AJAX request
I have this AJAX request:
$("#searchagain_form").submit(function() {
$.ajax({
type: 'POST',
url: "/searchforward",
data: {engine: $("#engine_options").find(".on_engine").text()}
});
});
This is my form:
<%= form_for :searchagain, :url => {:controller => 'results', :action => 'searchforward'}, :html => { :id => "searchagain_form" } do |f| %>
<%= f.text_field :search, :value => @search, :id => 'searchagain_bar' %>
<%= f.submit :value => 开发者_Go百科"search!", :id => 'searchagain_submit' %>
<% end %>
However, checking my logs, the data does not seem to be POSTed to the server when the form is submitted. Any idea why?
You did not cancel the submission, so the page will unload instead of sending the ajax-request.
Append a return false;
to the function.
But however, it's not clear what kind of element #engine_options .on_engine
is, if it is an text-input use val()
instead of text()
(text() should return nothing for text-inputs).
When it is a textarea it will return the textNode inside the element, not any later modified text. So use val() also for textarea.
You don't need a semicolon here:
{engine: $("#engine_options").find(".on_engine").text();}
Other than that, you can use a traffic monitor like Charles to monitor the post and make sure it's sending what you want it to.
Edit:
$('#searchagain_form').serialize()
will also give you an object with all your form data
Why don't you add an error handler in your ajax params?
$("#searchagain_form").submit(function() {
$.ajax({
type: 'POST',
url: "/searchforward",
data: {engine: $("#engine_options").find(".on_engine").text()},
error: function(jqXHR, textStatus, errorThrown){
console.log(errorThrown);
}
});
});
精彩评论