【问题标题】:HttpRequest.setRequestHeader generates DOM Exception 11 on DartHttpRequest.setRequestHeader 在 Dart 上生成 DOM 异常 11
【发布时间】:2012-12-10 15:48:27
【问题描述】:

我正在关注this tutorial,以便从 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....

阅读this question 后,我尝试将标题更改为“Content-Type: application/json”,然后尝试调用overriedMimeType method,但它给了我同样的错误。

req.overrideMimeType("application/json");

我也调用了setRequestHeader method,代码如下:

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)

感谢任何帮助。提前致谢。

【问题讨论】:

    标签: ruby-on-rails post httprequest dart


    【解决方案1】:

    哦,好吧,我找到了我的问题的答案...实际上setRequestHeader需要在req.open之后调用,所以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
    
    }
    

    【讨论】:

    • 是的,标准的 XMLHttpRequest 并不是很容易使用。很高兴你明白了。
    【解决方案2】:

    @colored_rectangle = ColoredRectangle.new(params[:colored_rectangle]) 上,在您的create 方法中,您将数据发送到ColoredRectangle 的构造函数,但您是通过使用根:colored_rectangle 来完成的。您发送的 json 中不存在此根。要修复它,只需将根添加到 json 中,如下所示:String jsonData = '{"colored_rectangle": {"color":"blue","x":"100","y":"100"}}';

    【讨论】:

    • 即使我将 jsonData 变量更改为包含标头,它也不起作用。但是,如果我在 open 方法之后添加 req.setRequestHeader,它确实有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-14
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多