2016-04-12 11 views
2

まだコントローラで動作するように私のajaxを修正しようとしています。 は、今私は、このエラーメッセージが出ます:Json parseerror&予期しないトークン

response SyntaxError: Unexpected token j , xhr[object Object] , STATUS parsererror 

config.routesを

resources :books do 
    member do 
     get '/last_chapter/', to: 'chapters#last_chapter', as: 'last_chapter', defaults: { format: 'json' } 
    end 
    resources :chapters 
    end 

章コントローラ

def last_chapter 
     @last_chapter = Chapter.order(created_at: :desc).limit(1) 
     respond_to do |format| 
      format.json 
     end 
    end 

last_chapter.json

json.extract! @last_chapter, :id, :title, :characters, :created_at, :updated_at 

script.js

$.ajax({ 
     type: "GET", 
     url: '/books/103/last_chapter.json', 
     contentType: "application/json", 
     success: function(data) { 
     console.log('JAAAA ENDELIG FUNKER DET'); 
     $("body").html(data); 
     }, 
     error: function(xhr, status, response) { 
     console.log('response ' + response + ' , xhr' + xhr + ' , ' + 'STATUS ' + status)} 
    }); 

私はすべてを試したように感じます。もう情報が必要な場合は教えてください! :)

答えて

1

あなたの問題はルートにあると思う。ここで私はどうなるのかです:あなたは、AJAXのURLを行うだろうどのように

class Book < ActiveRecord::Base 
    has_many :chapters 
end 
+0

resources :books do get :last_chapter, on: :member end 

books_controller.rb

def last_chapter @last_chapter = Book.find(params[:id]).chapters.last respond_to do |format| format.json do render json: { does_it_work: :yes } end end end 

私は、次のようなモデルを想定しますか?今のところ私は最後のチャプターIDが必要なので "/ last_chapter"を見つけることはできません。 – Cinta

+0

私はそのままajax呼び出しを残します。 'last_chapter'アクションのポイントは何ですか?そのidを知る必要がある場合はどうすればいいですか?あなたは 'book_id'を提供する必要があり、このアクションはこの本の最後の章を返すべきです! – born4new

+0

また、私はこのような章のIDを持つURLを手動で追加します。 URL:「/books/103/chapters/206/last_player.json」、 私はこのエラーを取得:storage.getに応答して 'エラー:例外TypeError:オブジェクトにヌル のプロパティ「スタイル」を読み取ることができません。コールバック(chrome-extension://kchdfagjljmhgapoonapmfngpadcjkhk/js/scripts.js:24:42) クロム拡張://kchdfagjljmhgapoonapmfngpadcjkhk/js/scripts.js:11:21' – Cinta

関連する問題