【问题标题】:Submitting POST form data from a Titanium Mobile App to a Ruby on Rails Web App将 POST 表单数据从 Titanium 移动应用程序提交到 Ruby on Rails Web 应用程序
【发布时间】:2013-09-04 20:22:28
【问题描述】:

我正在尝试将表单数据从 Titanium 移动应用程序提交到 Ruby on Rails Web 应用程序。它是一个包含基本数据和图像的 POST 多部分请求。

我将展示我尝试提交数据的不同方式,以及我从 RoR 日志中得到的错误。

一些细节:

  • problem[avatar] 的图像来自手机摄像头。
  • 以下代码对以下所有 sn-ps 通用。它出现在 sn-ps。

通用代码

var url = "http://some_RoR_app.herokuapp.com/problems";
var client = Ti.Network.createHTTPClient({
    // function called when the response data is available
    onload : function(e) {
        alert('success');
    },
    // function called when an error occurs, including a timeout
    onerror : function(e) {
        alert('error');
    },
    timeout : 60 * 1000
});

方式#1

我已将表单数据保存在一个变量中,然后将该变量传递给send() 函数。

var params = {
    'problem[avatar]': $.report_image_view.reportPhoto.toImage(), // returns a [Ti.Blob] object
    'problem[title]': $.report_title_view.title.value,
    'problem[description]': $.report_description_view.description.value,
    'problem[ptype]': $.report_type_view.report_type.value['valueID'],
    'problem[status]': 1,
    'problem[priority]': 2,
    'problem[latitude]': latitude,
    'problem[longitude]': longitude,
    'problem[user_id]': 2
}

// Prepare the connection.
client.open("POST", url);
client.send(params);        

RoR 日志

013-09-04T18:30:54.048412+00:00 app[web.1]: Started POST "/problems" for 111.222.333.444 at 2013-09-04 18:30:54 +0000
2013-09-04T18:30:54.248656+00:00 app[web.1]: Processing by ProblemsController#create as MULTIPART_FORM
2013-09-04T18:30:54.248656+00:00 app[web.1]: [paperclip] Saving attachments.
2013-09-04T18:30:54.248656+00:00 app[web.1]: Redirected to https://km7.herokuapp.com/problems/32
2013-09-04T18:30:54.248656+00:00 app[web.1]: Completed 302 Found in 194ms (ActiveRecord: 80.2ms)
2013-09-04T18:30:54.252433+00:00 heroku[router]: at=info method=POST path=/problems host=some_RoR_app.herokuapp.com fwd="111.222.333.444" dyno=web.1 connect=2ms service=208ms status=302 bytes=103
2013-09-04T18:30:55.157538+00:00 app[web.1]: Started POST "/problems/32" for 111.222.333.444 at 2013-09-04 18:30:55 +0000

结果

帖子已完成,但未达到预期。一条新记录被添加到数据库中,但它是空的。换句话说,图片没有上传,标题为空,描述、坐标和其他所有内容也是如此。只设置了时间戳和(对象)id。


方式#2

我所做的是,我没有将表单数据保存在变量中并传递该变量,而是使用表单数据作为传递给函数的参数创建了对象。

client.open("POST", url);
client.send({
    "problem[avatar]" : $.report_image_view.reportPhoto.toImage(), // returns a [Ti.Blob] object
    "problem[title]" : $.report_title_view.title.value,
    "problem[description]" : $.report_description_view.description.value,
    "problem[ptype]" : $.report_type_view.report_type["valueId"],
    "problem[status]" : 1,
    "problem[priority]" : 2,
    "problem[latitude]" : latitude,
    "problem[longitude]" : longitude,
    "problem[user_id]" : 2
}); 

RoR 日志

2013-09-04T18:48:48.328758+00:00 app[web.1]: Started POST "/problems" for 111.222.333.444 at 2013-09-04 18:48:48 +0000
2013-09-04T18:48:48.635395+00:00 app[web.1]: Processing by ProblemsController#create as MULTIPART_FORM
2013-09-04T18:48:48.635395+00:00 app[web.1]: [paperclip] Saving attachments.
2013-09-04T18:48:48.635395+00:00 app[web.1]: Redirected to https://km7.herokuapp.com/problems/34
2013-09-04T18:48:48.635395+00:00 app[web.1]: Completed 302 Found in 299ms (ActiveRecord: 66.1ms)
2013-09-04T18:48:48.644302+00:00 heroku[router]: at=info method=POST path=/problems host=some_RoR_app.herokuapp.com fwd="111.222.333.444" dyno=web.1 connect=2ms service=325ms status=302 bytes=103

结果

和以前一样。帖子已完成,但未达到预期。一条新记录被添加到数据库中,但它是空的。


方式#3

这一次我传递一个带有参数的字符串,就好像它是一个 GET 一样。但是,我不能以这种方式传递图像......或者我可以吗?

var post_data =  "problem[title]=Report rest client&problem[latitude]=18.09&problem[longitude]=-67.12&problem[ptype]=1&problem[status]=1&problem[priority]=2&problem[description]=Test description from rest client&problem[user_id]=2&problem[address]=123 Main Street, PR";

// Prepare the connection.
client.open("POST", url);           
client.send(post_data);  

RoR 日志

我不知道为什么,但它不断将我重定向到 /problems 直到它放弃。

2013-09-04T19:03:30.982008+00:00 heroku[router]: at=info method=POST path=/problems host=some_RoR_app.herokuapp.com fwd="111.222.333.444" dyno=web.1 connect=2ms service=10ms status=307 bytes=0
2013-09-04T19:03:36.415999+00:00 app[web.1]: Started POST "/problems" for 111.222.333.444 at 2013-09-04 19:03:36 +0000
2013-09-04T19:03:36.475568+00:00 heroku[router]: at=info method=POST path=/problems host=some_RoR_app.herokuapp.com fwd="111.222.333.444" dyno=web.1 connect=1ms service=64ms status=302 bytes=100
2013-09-04T19:03:36.476626+00:00 app[web.1]: Processing by ProblemsController#create as JS
2013-09-04T19:03:36.476626+00:00 app[web.1]:   Parameters: {"problem"=>{"title"=>"Report rest client", "latitude"=>"18.09", "longitude"=>"-67.12", "ptype"=>"1", "status"=>"1", "priority"=>"2", "description"=>"Test description from rest client", "user_id"=>"2", "address"=>"Calle 3, Bo. Coto Sur"}}
2013-09-04T19:03:36.476626+00:00 app[web.1]: Redirected to https://some_RoR_app.herokuapp.com/problems
2013-09-04T19:03:36.476626+00:00 app[web.1]: Completed 302 Found in 54ms (ActiveRecord: 26.1ms)
2013-09-04T19:03:37.428602+00:00 app[web.1]: Started POST "/problems" for 111.222.333.444 at 2013-09-04 19:03:37 +0000
2013-09-04T19:03:37.532547+00:00 app[web.1]: Processing by ProblemsController#create as JS
2013-09-04T19:03:37.532547+00:00 app[web.1]:   Parameters: {"problem"=>{"title"=>"Report rest client", "latitude"=>"18.09", "longitude"=>"-67.12", "ptype"=>"1", "status"=>"1", "priority"=>"2", "description"=>"Test description from rest client", "user_id"=>"2", "address"=>"Calle 3, Bo. Coto Sur"}}
2013-09-04T19:03:37.532547+00:00 app[web.1]: Redirected to https://some_RoR_app.herokuapp.com/problems
2013-09-04T19:03:37.532547+00:00 app[web.1]: Completed 302 Found in 92ms (ActiveRecord: 61.0ms)
2013-09-04T19:03:38.457108+00:00 heroku[router]: at=info method=POST path=/problems host=some_RoR_app.herokuapp.com fwd="111.222.333.444" dyno=web.1 connect=2ms service=84ms status=302 bytes=100
2013-09-04T19:03:38.378766+00:00 app[web.1]: Started POST "/problems" for 111.222.333.444 at 2013-09-04 19:03:38 +0000
2013-09-04T19:03:38.455542+00:00 app[web.1]: Processing by ProblemsController#create as JS
2013-09-04T19:03:38.455542+00:00 app[web.1]:   Parameters: {"problem"=>{"title"=>"Report rest client", "latitude"=>"18.09", "longitude"=>"-67.12", "ptype"=>"1", "status"=>"1", "priority"=>"2", "description"=>"Test description from rest client", "user_id"=>"2", "address"=>"Calle 3, Bo. Coto Sur"}}
2013-09-04T19:03:38.455542+00:00 app[web.1]: Redirected to https://some_RoR_app.herokuapp.com/problems
2013-09-04T19:03:38.455542+00:00 app[web.1]: Completed 302 Found in 67ms (ActiveRecord: 48.9ms)
2013-09-04T19:03:39.319540+00:00 app[web.1]: Started POST "/problems" for 111.222.333.444 at 2013-09-04 19:03:39 +0000
2013-09-04T19:03:39.361228+00:00 app[web.1]: Processing by ProblemsController#create as JS
2013-09-04T19:03:39.361228+00:00 app[web.1]:   Parameters: {"problem"=>{"title"=>"Report rest client", "latitude"=>"18.09", "longitude"=>"-67.12", "ptype"=>"1", "status"=>"1", "priority"=>"2", "description"=>"Test description from rest client", "user_id"=>"2", "address"=>"Calle 3, Bo. Coto Sur"}}
2013-09-04T19:03:39.361228+00:00 app[web.1]: Redirected to https://some_RoR_app.herokuapp.com/problems
2013-09-04T19:03:39.361228+00:00 app[web.1]: Completed 302 Found in 38ms (ActiveRecord: 22.6ms)
2013-09-04T19:03:39.630525+00:00 app[web.1]: Started POST "/problems" for 111.222.333.444 at 2013-09-04 19:03:39 +0000
2013-09-04T19:03:39.731993+00:00 app[web.1]: Processing by ProblemsController#create as JS
2013-09-04T19:03:39.731993+00:00 app[web.1]:   Parameters: {"problem"=>{"title"=>"Report rest client", "latitude"=>"18.09", "longitude"=>"-67.12", "ptype"=>"1", "status"=>"1", "priority"=>"2", "description"=>"Test description from rest client", "user_id"=>"2", "address"=>"Calle 3, Bo. Coto Sur"}}
2013-09-04T19:03:39.731993+00:00 app[web.1]: Redirected to https://some_RoR_app.herokuapp.com/problems
2013-09-04T19:03:39.731993+00:00 app[web.1]: Completed 302 Found in 84ms (ActiveRecord: 37.1ms)
2013-09-04T19:03:39.741297+00:00 heroku[router]: at=info method=POST path=/problems host=some_RoR_app.herokuapp.com fwd="111.222.333.444" dyno=web.1 connect=2ms service=117ms status=302 bytes=100

结果

我没有在数据库中看到任何更改。什么都没有保存。


方式#4

这次我以不同的方式组织 params 对象。否则,类似于Way #1

var params = 
    { problem : {
        avatar : $.report_image_view.reportPhoto.toImage(),
        title : $.report_title_view.title.value,
        description : $.report_description_view.description.value,
        ptype : $.report_type_view.report_type.value['valueID'],
        status : 1,
        priority : 2,
        latitude : latitude,
        longitude : longitude,
        user_id : 2     
    }}


// Prepare the connection.
client.open("POST", url);
client.send(params);   

RoR 日志

2013-09-04T19:19:38.381670+00:00 heroku[router]: at=info method=POST path=/problems host=some_RoR_app.herokuapp.com fwd="111.222.333.444" dyno=web.1 connect=4ms service=11ms status=307 bytes=0
2013-09-04T19:19:41.117687+00:00 app[web.1]: Started POST "/problems" for 111.222.333.444 at 2013-09-04 19:19:41 +0000
2013-09-04T19:19:41.162099+00:00 app[web.1]: NoMethodError (undefined method `stringify_keys' for #<String:0x00000003672638>):
2013-09-04T19:19:41.162099+00:00 app[web.1]:   app/controllers/problems_controller.rb:29:in `new'
2013-09-04T19:19:41.162099+00:00 app[web.1]:   app/controllers/problems_controller.rb:29:in `create'
2013-09-04T19:19:41.162099+00:00 app[web.1]: 
2013-09-04T19:19:41.162099+00:00 app[web.1]: 
2013-09-04T19:19:41.162726+00:00 app[web.1]: Processing by ProblemsController#create as URL_ENCODED_FORM
2013-09-04T19:19:41.162726+00:00 app[web.1]:   Parameters: {"problem"=>"{\n    avatar = \"[object TiBlob]\";\n    description = \"Enter a description\";\n    latitude = \"37.33159255981445\";\n    longitude = \"-122.0305099487305\";\n    priority = 2;\n    status = 1;\n    title = \"A test title\";\n    \"user_id\" = 2;\n}"}
2013-09-04T19:19:41.162726+00:00 app[web.1]: Completed 500 Internal Server Error in 39ms
2013-09-04T19:19:41.164682+00:00 heroku[router]: at=info method=POST path=/problems host=some_RoR_app.herokuapp.com fwd="111.222.333.444" dyno=web.1 connect=2ms service=50ms status=500 bytes=643 

结果 数据库中没有保存任何内容。但是,这一次它在 Problems 控制器的 newcreate 方法中显式生成了 NoMethodError


方式#5

这一次我像 Way #4 一样创建对象,但直接将它作为参数传递给 send() 方法,就像 Way #3 一样。

// Prepare the connection.
client.open("POST", url);
client.send({
    problem : {
        avatar : $.report_image_view.reportPhoto.toImage(),
        title : $.report_title_view.title.value,
        description : $.report_description_view.description.value,
        ptype : $.report_type_view.report_type.value['valueID'],
        status : 1,
        priority : 2,
        latitude : latitude,
        longitude : longitude,
        user_id : 2 
}}); 

RoR 日志

2013-09-04T20:16:36.865831+00:00 heroku[router]: at=info method=POST path=/problems host=some_RoR_app.herokuapp.com fwd="111.222.333.444" dyno=web.1 connect=2ms service=3ms status=307 bytes=0
2013-09-04T20:16:38.772048+00:00 app[web.1]: Started POST "/problems" for 111.222.333.444 at 2013-09-04 20:16:38 +0000
2013-09-04T20:16:38.808102+00:00 app[web.1]: NoMethodError (undefined method `stringify_keys' for #<String:0x00000005d45f58>):
2013-09-04T20:16:38.808102+00:00 app[web.1]:   app/controllers/problems_controller.rb:29:in `new'
2013-09-04T20:16:38.808102+00:00 app[web.1]:   app/controllers/problems_controller.rb:29:in `create'
2013-09-04T20:16:38.808102+00:00 app[web.1]: 
2013-09-04T20:16:38.808102+00:00 app[web.1]: 
2013-09-04T20:16:38.808786+00:00 app[web.1]: Processing by ProblemsController#create as URL_ENCODED_FORM
2013-09-04T20:16:38.808786+00:00 app[web.1]:   Parameters: {"problem"=>"{\n    avatar = \"[object TiBlob]\";\n    description = \"Fix it asap.\";\n    latitude = \"37.33069610595703\";\n    longitude = \"-122.0306701660156\";\n    priority = 2;\n    status = 1;\n    title = \"A broken water pipe\";\n    \"user_id\" = 2;\n}"}
2013-09-04T20:16:38.808786+00:00 app[web.1]: Completed 500 Internal Server Error in 30ms
2013-09-04T20:16:38.809384+00:00 heroku[router]: at=info method=POST path=/problems host=some_RoR_app.herokuapp.com fwd="111.222.333.444" dyno=web.1 connect=2ms service=41ms status=500 bytes=643

结果

方式#4

相同的错误

这让我发疯了。

你知道我做错了什么吗?我怎样才能解决这个问题?我很乐意回答任何可以帮助您的问题。只需发表评论。

【问题讨论】:

  • 你有没有想过这个问题?在我的应用程序中遇到完全相同的问题。
  • @Austio:这是我现在的代码:pastie.org/private/2pake38otcqwh8atuhw 它永远不会运行client.onload(),因为服务器永远不会返回 200 HTTP 响应。它返回一个 302 代码,这实际上是我想要的,但 Titanium 将其视为错误,这就是它执行 client.onerror() 的原因。我从来没有真正理解我是如何让它工作的。抱歉,我无法真正解释这一点。祝你好运!
  • 找到解决办法了吗??在这里遇到同样的问题...

标签: ruby-on-rails forms titanium titanium-mobile titanium-alloy


【解决方案1】:

尝试添加:

client.setRequestHeader('enctype', 'multipart/form-data');

在打开函数之前。

【讨论】:

    猜你喜欢
    • 2013-10-11
    • 1970-01-01
    • 2015-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多