[英]Can't get Json data to Ajax in Rails 5
嗨,我試圖用Ajax理解和實現Rails動作控制器。 我的問題是為什么我無法獲取Json數據並將其顯示在我的Ajax函數中的console.log中。 我將ajax函數放在application.js文件中。 我的ajax get方法工作正常,但post方法似乎不起作用。 在文章索引頁面中,當您單擊新文章時,它將呈現部分格式,並且ajax將與控制器中的create動作進行通信,以將數據追加到索引頁面。 但是我無法將Json數據解析到Ajax發布。 這是我的代碼文件: 更新的圖像 在此處輸入圖像描述當我單擊“網絡”選項卡中的“僅創建json數據”時。 Alert和console.log沒有顯示任何內容。
application.js
$(function (){
var $article_table = $('#article_table');
$.ajax({
type: 'GET',
dataType: 'json',
url: '/articles',
success: function(articles){
$.each(articles, function(i, article) {
$article_table.append('<tr><td>'+article.name+'</td></tr>')
});
}
});
$('#form_article').submit(function(){
//var name = $('#article_name').val();
var name = JSON.parse(xhr.responseText);
$.ajax({
type: 'POST',
url: '/articles',
dataType: 'json',
data: name,
success: function(data){
//alert(data.id);
console.log('success',data);
//debugger;
},
error: function(){
}
});
});
Articles_controller.rb =>在創建控制器中,我將文章渲染為json
class ArticlesController < ApplicationController
before_action :set_article, only: [:show, :edit, :update, :destroy]
# GET /articles
# GET /articles.json
def index
@articles = Article.all
end
def show
end
# GET /articles/new
def new
@article = Article.new
end
# GET /articles/1/edit
def edit
end
# POST /articles
# POST /articles.json
def create
@article = Article.new(article_params)
#respond_to do |format|
if @article.save
#format.html { redirect_to @article, notice: 'Article was successfully created.' }
#format.json { render :show, status: :created, location: @article }
#format.html
#format.json { render json: @article, status: :ok}
render json: @article, status: :ok
else
#format.html { render :new }
#format.html
render json: @article.errors, status: :unprocessable_entity
#format.json { render json: @article.errors, status: :unprocessable_entity }
end
#end
end
_form.html.erb:
<%= form_with(model: @article, remote: true , id: :form_article) do |form| %>
<% if @article.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@article.errors.count, "error") %> prohibited this article from being saved:</h2>
<ul>
<% @article.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :name %>
<%= form.text_field :name, id: :article_name %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
index.html.erb:
<p id="notice"><%= notice %></p>
<h1>Articles</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody id="article_table">
</tbody>
</table>
<br>
<%= link_to 'New Article', new_article_path, id: :new_article_link, remote: true %>
new.js.erb:
$('#new_article_link').hide().after("<%= j render 'form' %>");
您需要像這樣傳遞數據
data: {article: {name: name}}
在jQuery / AJAX部分上,請參見以下內容
$('#form_article').submit(function(){
// Declare a variable for get data from input field
var name = $('#article_name').val();
$.ajax({
type: 'POST',
url: '/articles',
dataType: 'json',
data: {article: {name: name}},
success: function(data){
//alert(data.id);
//console.log('success', data);
console.log(JSON.stringify(data));
//debugger;
},
error: function(){
alert("Error");
}
});
});
希望能幫助到你
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.