Dan*_*nny 2 ruby rest ruby-on-rails ruby-on-rails-3
我创建了一个简单的api,用json响应.当我尝试从浏览器调用它时,我得到了相应的json响应,但是当我尝试从actionscript远程调用它时,似乎它试图调用json就好像它是一个方法.这是控制器动作:
def matcher
@conn = Connection.first
if(@conn)
respond_with({:conn => @conn, :status => :ok}.to_json)
else
respond_with({:status => :no_content}.to_json)
end
end
Run Code Online (Sandbox Code Playgroud)
这是接到电话时的服务器响应
Started POST "/connection/matcher/1.json" for 127.0.0.1 at 2010-11-11 22:57:24 -0800
Processing by ConnectionsController#matcher as JSON
Parameters: {"stratus_string"=>"bad1de003755eaa01a2920f0091d0dd66beaf2d34f651b09a578afb1b54e5686", "user_id"=>"1", "id"=>"1"}
Connection Load (0.5ms) SELECT "connections".* FROM "connections" LIMIT 1
Completed in 24ms
NoMethodError (undefined method `{"conn":{"created_at":"2010-11-12T06:55:13Z","id":6,"stratus_string":"474735730abe81d7622d377bd0bf816c3f94721ece3eddf670bf3a74b1c2356c","updated_at":"2010-11-12T06:55:13Z","user_id":1},"status":"ok"}_url' for #<ConnectionsController:0x000001030e4350>):
app/controllers/connections_controller.rb:7:in `matcher'
Run Code Online (Sandbox Code Playgroud)
为什么rails试图执行json响应?我不明白.
更新:
这是进行调用的actionscript代码,虽然我不明白为什么它会产生影响.
this.restService = new HTTPService();
this.restService.url = "http://localhost:3000/connection/matcher/1.json";
this.restService.method = "POST";
this.restService.addEventListener("result", onRestResult);
this.restService.addEventListener("fault", onRestFault);
var request:Object = new Object();
request.user_id = user;
request.stratus_string = id;
this.restService.cancel();
this.restService.send(request);
Run Code Online (Sandbox Code Playgroud)
谢谢!
我很好奇为什么它在浏览器中有效.我希望它也能在那里失败.检查来自respond_with的来源:
def respond_with(*resources, &block)
raise "In order to use respond_with, first you need to declare the formats your " <<
"controller responds to in the class level" if self.class.mimes_for_respond_to.empty?
if response = retrieve_response_from_mimes(&block)
options = resources.extract_options!
options.merge!(:default_response => response)
(options.delete(:responder) || self.class.responder).call(self, resources, options)
end
end
Run Code Online (Sandbox Code Playgroud)
显而易见,它试图将其用作方法.我想你一直坚持:
respond_to do |format|
format.html # some.html.erb
format.json { render :json => {:conn => @conn, :status => :ok}.to_json }
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6432 次 |
| 最近记录: |