1

dart から ruby​​ on rails バックエンド アプリケーションに POST メソッドを実行するために、このチュートリアルに従っています。そのため、チュートリアルに示されているように、最初は URL と JSON データを変更するだけでコードを試しました。

void main() {   
  String jsonData = '{"color":"blue","x":"100","y":"100"}'; 
  saveData(jsonData, onSuccess); // send the data to  // the server
}

void onSuccess(HttpRequest req) {
    print(req.responseText); // print the received raw JSON text
}

void saveData(String data, onSuccess(HttpRequest req)) {
  HttpRequest req = new HttpRequest(); // create a new XHR

  // add an event handler that is called when the request finishes
  req.on.readyStateChange.add((Event e) {
    if (req.readyState == HttpRequest.DONE &&
        (req.status == 200 || req.status == 0)) {
      onSuccess(req); // called when the POST successfully completes
    }
  });

  var url = "http://localhost:3030/colored_rectangles.json";
  req.open("POST", url); // Use POST http method to send data in the next call
  req.send(data); // kick off the request to the server

}

これが私のコントローラーのメソッドとRubyの私のモデルです(非常にシンプルで、足場で生成されます):

  # POST /colored_rectangles
  # POST /colored_rectangles.json
  def create
    @colored_rectangle = ColoredRectangle.new(params[:colored_rectangle])

    respond_to do |format|
      if @colored_rectangle.save
        format.html { redirect_to @colored_rectangle, notice: 'Colored rectangle was successfully created.' }
        format.json { render json: @colored_rectangle, status: :created, location: @colored_rectangle }
      else
        format.html { render action: "new" }
        format.json { render json: @colored_rectangle.errors, status: :unprocessable_entity }
      end
    end
  end
class ColoredRectangle < ActiveRecord::Base
  attr_accessible :color, :x, :y
end

コードを実行すると、Dart で次のエラーが表示されます。

 Failed to load resource: the server responded with a status of 500 (Internal Server Error)
  http://localhost:3030/colored_rectangles.json

そしてRailsのログイン:

REXML::ParseException (The document "{\"color\":\"blue\",\"x\":\"100\",\"y\":\"100\"}" does not have a valid root):
 activesupport (3.2.9) lib/active_support/xml_mini/rexml.rb:35:in `parse'
 C:in `parse'
  etc....

この質問を読んだ後、ヘッダーを「Content-Type: application/json」に変更しようとした後、overriedMimeType メソッドを呼び出そうとしましたが、同じエラーが発生します。

req.overrideMimeType("application/json");

次のコードでsetRequestHeader メソッドも呼び出しました。

void saveLanguageData(String data, onSuccess(HttpRequest req)) {
  HttpRequest req = new HttpRequest(); // create a new XHR

  // add an event handler that is called when the request finishes
  req.on.readyStateChange.add((Event e) {
    if (req.readyState == HttpRequest.DONE &&
        (req.status == 200 || req.status == 0)) {
      onSuccess(req); // called when the POST successfully completes
    }
  });

  var url = "http://localhost:3030/colored_rectangles.json";
  req.setRequestHeader("Content-type", "application/json"); //This was added
  req.open("POST", url); // Use POST http method to send data in the next call
  req.send(data); // kick off the request to the server

}

しかし、Dart から次のエラーが表示されます。

Exception: Error: INVALID_STATE_ERR: DOM Exception 11
Stack Trace: #0      HttpRequest.setRequestHeader (E:\b\build\slave\dartium-win-full-trunk\build\src\build\Release\obj\global_intermediate\webkit\bindings\dart\dart\html\HttpRequest.dart:34:1)
#1      saveLanguageData (http://localhost:3030/rademo_dart/web/rademo.dart:45:23)
#2      main (http://localhost:3030/rademo_dart/web/rademo.dart:10:19)

どんな助けでも大歓迎です。前もって感謝します。

4

2 に答える 2

5

まあ、私は私の問題に対する答えを見つけました...実際には、req.open の後に setRequestHeader を呼び出す必要があるため、saveData メソッドのコードは次のようになります。

void saveData(String data, onSuccess(HttpRequest req)) {
  HttpRequest req = new HttpRequest(); // create a new XHR

  // add an event handler that is called when the request finishes
  req.on.readyStateChange.add((Event e) {
    if (req.readyState == HttpRequest.DONE &&
        (req.status == 200 || req.status == 0)) {
      onSuccess(req); // called when the POST successfully completes
    }
  });

  var url = "http://localhost:3030/colored_rectangles.json";
  req.open("POST", url); // Use POST http method to send data in the next call
  req.setRequestHeader("Content-type", "application/json");
  req.send(data); // kick off the request to the server

}
于 2012-12-10T16:28:15.633 に答える
0

@colored_rectangle = ColoredRectangle.new(params[:colored_rectangle])では、メソッドでデータをColoredRectanglecreateのコンストラクターに送信していますが、ルートを使用してそれを行っています:colored_rectangle。このルートは、送信した json には存在しません。これを修正するには、次のようにルートを json に追加します。String jsonData = '{"colored_rectangle": {"color":"blue","x":"100","y":"100"}}';

于 2012-12-10T16:30:24.137 に答える