LoginSignup
0
0

More than 5 years have passed since last update.

Ajaxでデータをコントローラ渡すときにJSON::ParserError

Posted at

Ajaxから渡されるdataをparseするとJSON::ParserError

app/assets/javascripts/user.coffee
UserEvent
  data =
     user:
       name: "hoge"
       age: 25
  $.ajax
    type: 'POST'
    url: /user/create
    data: data
    dataType: 'json'

するとこのようなエラーがでる。JSON::ParserError
原因ははっきりわからなかったが、json内の数値がうまくdecodeできていないっぽい?
どなたかわかる方は教えてください
Json.stringifyを利用し、contentTypeを指定すればちゃんとdecodeできるようになる

app/assets/javascripts/user.coffee
UserEvent
  data =
     JSON.stringify(
       user:
         name: "hoge"
         age: 25
     )
  $.ajax
    type: 'POST'
    url: /user/create
    data: data
    dataType: 'json'
    contentType: 'application/json'

参考URL
http://stackoverflow.com/questions/6410810/rails-not-decoding-json-from-jquery-correctly-array-becoming-a-hash-with-intege

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0