簡體   English   中英

Ruby on Rails中的Ajax不發送數據

[英]ajax in ruby on rails doesn t send the data

我正在對ajax進行測試(因為我對ajax非常陌生)是將數據從view welcome(index.html.erb)視圖發送到控制器(welcome_controller.rb),但我嘗試過的所有時間都向我展示了相同的情況錯誤:

在此處輸入圖片說明 具有相同的更改: 在此處輸入圖片說明

但是我不知道這是什么意思,而且在我執行請求時,視圖始終向我顯示警報“錯誤”(錯誤)。

這是我在視圖中所做的:

   <%= javascript_include_tag "application" %>

    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>

    <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet">

    <script src="//netdna.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>

$(document).ready(function(){
    $("#petition").click(function(){
        $.ajax({
            url: 'http://192.168.99.100:3000/welcome_controller/index/',
            type: 'POST',    
            dataType: 'json',
            data: { max_id: {id: "5"} },
            success: function (data) {
                alert("success");
            },
            error: function(xhr, status, error) {

                alert(xhr+status+error);    \\to take the second photo i change this for this "alert(JSON.stringify(xhr));" 
            }           
        });
    });
});

這是我的routes.rb:

Rails.application.routes.draw do
  resources :welcome do
    collection do
      get 'index'
    end
  end

  get 'welcome/page'
  root 'welcome#index'
  get 'menu/index'
end

這是我application.js:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's
// vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery  
//= require jquery_ujs 
//= require tinymce
//= require rails-ujs
//= require turbolinks
//= require_tree .

這是紅寶石寶石:

# frozen_string_literal: true

source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?('/')
  "https://github.com/#{repo_name}.git"
end

#propiedades de usuario
gem 'devise', '~> 4.4', '>= 4.4.1'

#text editor to post with link and imagens
gem 'tinymce-rails'

#admin y moderator
gem 'cancancan'
gem 'rolify'

#votar like y dislike
gem 'acts_as_votable', '~> 0.11.1'

#consultas rails para mesclar con js
gem 'jquery-rails'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.5'
# Use postgresql as the database for Active Record
gem 'pg', '>= 0.18', '< 2.0'
# Use Puma as the app server
gem 'puma', '~> 3.7'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: %i[mri mingw x64_mingw]
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '~> 2.13'
  gem 'selenium-webdriver'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'listen', '>= 3.0.5', '< 3.2'
  gem 'web-console', '>= 3.3.0'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]

我的控制器名稱是:welcome_controller.rb並具有2個方法索引,表明我無論如何都會放置控制器:

class WelcomeController < ApplicationController
  before_action :set_welcome, only: [:show]
  protect_from_forgery :except => :receive_guest
  helper_method :current_or_guest_user

  def index
    puts params[:max_id]
  end

  def show
  end

end

當您的gem文件中包含jquery gem時,您可以使用

//= require jquery

在您的application.js中

您已經完成了。

您需要從視圖中刪除這些行

<script src="jquery-3.3.1.min.js"></script>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

並用這一行替換它們

<%= javascript_include_tag "application" %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM