首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在rails应用程序中发送AJAX Post Jquery

在rails应用程序中发送AJAX Post Jquery
EN

Stack Overflow用户
提问于 2013-07-10 06:46:21
回答 1查看 56.1K关注 0票数 18

使用简单的控制器:

  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错误URI `/products/object%20Object‘。

并且数据库中没有任何变化。为什么不是获取oobject uri,而是获取prducts/ /products之类的东西?有什么问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-10 06:56:47

试试这个:

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;
  }
})));
票数 34
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17559563

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档