我的RUBY之路--第五周(01) rails 后台接受Titanium android传过来的POST GET请求,设定端口

<span style="font-size:18px;">上面是定义接口,interface_show还未写好,app/controllers/interface_controller.rb 
</span>
<span style="font-size:18px;">  1 # -*- encoding : utf-8 -*-
  2 class InterfaceController < ApplicationController
  3 
  4   def create_user
  5     User.create email: params['email'], encrypted_password: params['password']
  6     render :text => 'ok!'
  7   end
  8 
  9   def interface_show
 10    # @message =
 11     render json: {
 12       result: @message.map{ |message|
 13         {
 14           id: message.id,
 15           name: message.name
 16         }
 17       },
 18       comment: '测试用接口,属性,id ,name:'
 19     }
 20   end
 21 end</span>



添加路由/workspace/work/rubyonrails/happystock1/config/routes.rb 

<span style="font-size:18px;"> 15   resources :interface do
 16     collection do
 17       get :interface_show
 18       get :create_user
 19       post :create_user
 20     end
 21   end
</span>

android中添加请求:

<span style="font-size:18px;">function get_pop_stock(e) {
	//取输入框里面的值
    //var input_name = $.input_name.value;
    //获取URL
    var url = "http://127.0.0.1:3000/interface_index";
	var client = Ti.Network.createHTTPClient({
		onload : function(e) {
			
			//解析Json
			var jsons = JSON.parse(this.responseText);
			var str = jsons.result;
			//更改text的值
			$.label.setText(str);
		},
		onerror : function(e) {
			Ti.API.debug(e.error);
			alert('error');
		},
		timeout : 5000 // in milliseconds

	});
	client.open("GET", url);
	client.send();
}</span>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值