IOS中请求服务器端方法
有两个输出口,分别为username和password,用来获取界面输入的用户名和密码,还有一个login方法,当点击登录按钮时,会调用该方法,将输入的用户名和密码发送到后台并返回数据。
- (IBAction) login { // 数据内容,存入字符串 NSString *requestString = [NSString stringWithFormat:@"&user_name=%@&pwd=%@", username.text, password.text]; // 数据内容转换为UTF8编码,第二个参数为数据长度 NSData *requestData = [NSData dataWithBytes:[requestString UTF8String] length:[requestString length]]; // 请求的URL地址 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://192.168.10.121:3000/users/login"]]; // 设置请求方式 [request setHTTPMethod:@"POST"]; // 设置请求内容 [request setHTTPBody:requestData]; // 设置请求头声明 [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"]; // 执行请求 NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; [request release]; // 输出返回数据 NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"%@", returnString); [returnString release]; }
后台方法如下:
def login if params[:user_name] == "root" && params[:pwd] == "root" respond_to do |format| format.json{ render :inline => User.all.to_json } end else render :inline => "failure" end end
相关推荐
wuychn 2020-08-16
阳光之吻 2020-08-03
sjpeter 2020-07-26
yang0cs 2020-07-04
furongwei 2020-07-04
goodriver 2020-06-17
MichelinMessi 2020-06-13
sapliang 2020-06-12
zhangwentaohh 2020-06-05
swtIrene 2020-06-04
SoarFly00 2020-06-03
TONIYH 2020-05-25
PioneerFan 2020-05-10
Vampor 2020-05-03
xcznb 2020-05-03
huangzonggui 2020-05-01
furongwei 2020-04-25
bluetears 2020-04-23
SoarFly00 2020-04-19