The content of the article is personal study notes. If there is any mistake, I hope you will criticize and correct it. I am grateful.
Ajax uses the get method to refresh the page (json format data return)
front end:
$.ajax ({
method: 'GET',
url: "/products/" + "hahaha" + "/biaodan",
dataType: 'json',
success: function(data){
alert(data.ip);
console.log(data);
}
});
Note: You need to specify the url, method, and data type to receive.
Backend:
respond_to do |format|
format.json {render json: {ip:@ip}}
end
The processed data ({ip:@ip}) can be used directly on the front end
Ajax uses post method (js form data return)
Front end: Basically no need to do anything, only need to process the returned data, how to perform data processing
<%= form_tag products_biaodan_path,:method=>"post",remote:true do%>
<%= label_tag("Search for:") %>
<%= text_field_tag(:aa,:name) %>
<%= password_field(:pwd,:password) %>
<%= submit_tag("Search") %>
<% end %>
remote: true is very important, it is responsible for the Ajax processing that is turned on
rear end:
def biaodan
@ip = params[:aa]+"666"
@pwd = params[:pwd][:password]
Rails.logger.info '=========+++++=============='+@ip
respond_to do |format|
format.js
end
end
In this way, @ip and @pwd will be directly transferred to a file named biaodan.js.erb, which can be processed directly.
Note: biaodan.js.erb is named by the method name def biaodan This is very important