gpt4 book ai didi

ruby-on-rails - HttpRequest.setRequestHeader在Dart上生成DOM异常11

转载 作者:行者123 更新时间:2023-12-03 03:11:05 25 4
gpt4 key购买 nike

我正在跟踪this tutorial,以便从dart到Rails后端应用程序上的ruby运行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

}

这是 Controller 中的方法和 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之后,我尝试将 header 更改为“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)

任何帮助表示赞赏。提前致谢。

最佳答案

哦,好了,我找到了解决问题的答案...实际上,需要在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

}

关于ruby-on-rails - HttpRequest.setRequestHeader在Dart上生成DOM异常11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13804294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com