gpt4 book ai didi

ruby-on-rails - 在rails应用程序中发送AJAX Post Jquery

转载 作者:行者123 更新时间:2023-12-03 21:50:47 25 4
gpt4 key购买 nike

使用简单的 Controller :

  def new
@product = Product.new
respond_to do |format|
format.html #new.html.erb
format.json { render json: @product}
end
end

def create
@product = Product.new(params[:product])
respond_to do |format|
if @product.save
format.html { redirect_to @product, notice: "Save process completed!" }
format.json { render json: @product, status: :created, location: @product }
else
format.html {
flash.now[:notice]="Save proccess coudn't be completed!"
render :new
}
format.json { render json: @product.errors, status: :unprocessable_entity}
end
end
end

和简单的ajax请求

$("h1").click ->
$.post
url: "/products/"
data:
product:
name: "Filip"
description: "whatever"

dataType: "json"
success: (data) ->
alert data.id

我正在尝试发送新产品,但服务器已应答

[2013-07-09 18:44:44] ERROR bad URI `/products/[object%20Object]'.

数据库中没有任何变化。为什么不是获取/products uri 而是获取 products/[oobject] 东西?有什么问题吗?

最佳答案

试试这个:

CoffeeScript

$ ->
$("h1").click ->
$.ajax({
type: "POST",
url: "/products",
data: { product: { name: "Filip", description: "whatever" } },
success:(data) ->
alert data.id
return false
error:(data) ->
return false
})

ES6 化

$(() => $("h1").click(() => $.ajax({
type: "POST",
url: "/products",
data: { product: { name: "Filip", description: "whatever" } },
success(data) {
alert(data.id);
return false;
},
error(data) {
return false;
}
})));

关于ruby-on-rails - 在rails应用程序中发送AJAX Post Jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17559563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com