2012-04-23 55 views
0

我正在嘗試從phonegap做一個簡單的發佈到rails 3.2.2 scaffold。我有一個簡單的腳手架,由一個控制器組成=>圖像和一個場=> t.string:名稱Phonegap Ajax Post to Rails 3.2.2獲取MultiJson :: DecodeError(756:意外標記

class ImagesController < ApplicationController 
    # GET /images 
    # GET /images.json 
    def index 
    @images = Image.all 

    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @images } 
    end 
    end 

    # GET /images/1 
    # GET /images/1.json 
    def show 
    @image = Image.find(params[:id]) 

    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @image } 
    end 
    end 

    # GET /images/new 
    # GET /images/new.json 
    def new 
    @image = Image.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @image } 
    end 
    end 

    # GET /images/1/edit 
    def edit 
    @image = Image.find(params[:id]) 
    end 

    # POST /images 
    # POST /images.json 
    def create 
    @image = Image.new(params[:image]) 

    respond_to do |format| 
     if @image.save 
     format.html { redirect_to @image, notice: 'Image was successfully created.' } 
     format.json { render json: @image, status: :created, location: @image } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @image.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PUT /images/1 
    # PUT /images/1.json 
    def update 
    @image = Image.find(params[:id]) 

    respond_to do |format| 
     if @image.update_attributes(params[:image]) 
     format.html { redirect_to @image, notice: 'Image was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @image.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /images/1 
    # DELETE /images/1.json 
    def destroy 
    @image = Image.find(params[:id]) 
    @image.destroy 

    respond_to do |format| 
     format.html { redirect_to images_url } 
     format.json { head :no_content } 
    end 
    end 
end 

從PhoneGap的應用index.html文件是:

<!DOCTYPE HTML> 
<html> 
<head> 
<title>PhoneGap</title> 
<script type="text/javascript" charset="utf-8" src="cordova-1.6.1.js"></script> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> 
<script type="text/javascript" charset="utf-8"> 

    function appReady(){ 

    var ajax = new XMLHttpRequest(); 
var data = '{ image: { name: "noway" } }'; 
    ajax.open("POST","http://<server>/images.json",true); 
    ajax.setRequestHeader("Content-type", "application/json") 
    ajax.send(data); 

    ajax.onreadystatechange=function(){ 
      if(ajax.readyState==4 && (ajax.status==200)){ 
       document.getElementById('main').innerHTML = ajax.responseText; 
      } 
    } 
} 


    document.addEventListener("deviceready", appReady, false); 

    </script> 
</head> 
<body> 

<h1>Hello World</h1> 
<div id="main"> 
</div> 
</body> 
</html> 

在WEBrick控制檯產生以下:

Started POST "/images.json" for 10.0.1.14 at 2012-04-22 22:32:58 -0500 
Error occurred while parsing request parameters. 
Contents: 

MultiJson::DecodeError (756: unexpected token at '{ image: { name: "noway" } }'): 
    json (1.6.6) lib/json/common.rb:148:in `parse' 

我認爲這是與我格式化JSON對象的方式,因爲當我改變:

var data = '{ image: { name: "noway" } }'; 

到:

var data = " "; 

我得到:

Started POST "/images.json" for 10.0.1.14 at 2012-04-22 22:57:03 -0500 
Processing by ImagesController#create as JSON 
    Parameters: {"image"=>{}} 
WARNING: Can't verify CSRF token authenticity 
    (0.1ms) begin transaction 
    SQL (24.1ms) INSERT INTO "images" ("created_at", "name", "updated_at") VALUES (?, ?, ?) [["created_at", Mon, 23 Apr 2012 03:57:03 UTC +00:00], ["name", nil], ["updated_at", Mon, 23 Apr 2012 03:57:03 UTC +00:00]] 
    (46.0ms) commit transaction 
Completed 201 Created in 76ms (Views: 1.9ms | ActiveRecord: 70.2ms) 

我在某種程度上錯過了json數據嗎?

回答

2

我能夠POST改變行:使用它生產的WEBrick以下結果的JSON字符串化方法

var data = JSON.stringify({ image: { name: "noway" } }); 

Started POST "/images.json" for 192.168.1.139 at 2012-04-23 17:54:58 -0500 
Processing by ImagesController#create as JSON 
    Parameters: {"image"=>{"name"=>"noway"}} 
WARNING: Can't verify CSRF token authenticity 
    (0.1ms) begin transaction 
    SQL (1.1ms) INSERT INTO "images" ("created_at", "name", "updated_at") VALUES (?, ?, ?) [["created_at", Mon, 23 Apr 2012 22:54:58 UTC +00:00], ["name", "noway"], ["updated_at", Mon, 23 Apr 2012 22:54:58 UTC +00:00]] 
    (45.8ms) commit transaction 
Completed 201 Created in 51ms (Views: 1.4ms | ActiveRecord: 47.0ms) 
+0

嘿實際上

var data = '{ image: { name: "noway" } }'; 

到這也是使它適用於application/json編碼所需要的。否則Rails會一直拋出MultiJSON解析錯誤。你必須收到你的答案upvote。 – sudhanshu 2013-03-19 08:22:11

+1

爲了說明更多,'{{image:{name:「noway」}}''的格式不正確。 JSON指定你也在你的密鑰周圍加引號。它需要如下所示:''{「image」:{「name」:「noway」}}''這是JSON.stringify將產生的內容。 – rescuecreative 2013-12-11 22:02:16