set*_*fri 1 post objective-c ios afnetworking
我正在尝试使用AFNetworking向服务器发送POST请求,一切似乎都在工作,即应用程序成功ping服务器.但是,当它到达服务器时,它发送的参数值是空白的,即使在使用调试器单步执行下面的代码之后,值似乎正在成功传递.任何有关这方面的帮助将不胜感激.
APIClient.m
#import "APIClient.h"
#import "AFJSONRequestOperation.h"
// Removed URL for privacy purposes.
static NSString * const kAPIBaseURLString = @"string goes here";
@implementation APIClient
+ (APIClient *)sharedClient {
static APIClient *_sharedClient;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedClient = [[APIClient alloc] initWithBaseURL:[NSURL URLWithString:kAPIBaseURLString]];
});
return _sharedClient;
}
- (id)initWithBaseURL:(NSURL *)url {
self = [super initWithBaseURL:url];
if (self) {
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
// Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
[self setDefaultHeader:@"Accept" value:@"application/json"];
}
return self;
}
@end
Run Code Online (Sandbox Code Playgroud)
LoginBrain.m中的登录方法
- (void)loginUsingEmail:(NSString *)email andPassword:(NSString *)password withBlock:(void (^)(NSDictionary *loginResults))block {
self.email = email;
self.password = password;
// Removed path for privacy purposes
[[APIClient sharedClient] postPath:@"insert path here" parameters:[NSDictionary dictionaryWithObjectsAndKeys:email, @"uname", password, @"pw", nil] success:^(AFHTTPRequestOperation *operation, id responseJSON) {
if (block) {
block(responseJSON);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
if (block) {
block(nil);
}
}];
// Store user data in app?
}
Run Code Online (Sandbox Code Playgroud)
在LoginViewController.m中登录被调用的方法
- (IBAction)loginPressed {
[self.loginProgressIndicator startAnimating];
NSString *email = self.emailTextField.text;
NSString *password = self.passwordTextField.text;
[self.brain loginUsingEmail:email andPassword:password withBlock:^(NSDictionary *loginResults) {
[self.loginProgressIndicator stopAnimating];
[self.delegate uloopLoginViewController:self didLoginUserWithEmail:email andPassword:password];
}];
}
Run Code Online (Sandbox Code Playgroud)
UPDATE
我尝试改变这里parameterEncoding推荐的,但它没有解决问题.
第二次更新
以下是服务器端访问POST数据的PHP代码.这是我的一个同事写的,因为我在服务器端没有做任何事情,我对它的工作原理非常不熟悉.
header('Content-type: application/json');
$username = $_POST['uname'];
$pw = $_POST['pw'];
Run Code Online (Sandbox Code Playgroud)
服务器代码非常简单.他有一些日志脚本可以检查变量值是什么,他说客户端正在访问服务器,但变量值是空白的.
第三次更新
这是通过产生一个HTTP请求的转储print_r的的$_REQUEST变量:
Array ( [sid] => FwAqvZrfckw )
Run Code Online (Sandbox Code Playgroud)
这是$_POST变量的转储.如你所见,它完全是空白的:
Array ( )
Run Code Online (Sandbox Code Playgroud)
第四次更新
在将数据包发送到服务器之前,我使用Wireshark来捕获数据包,并且所有内容都按顺序显示:
Accept: application/json
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Run Code Online (Sandbox Code Playgroud)
而且POST参数也都存在.我们还在服务器端创建了一个测试文件,并且只是进行了测试POST以确保其中的代码正常工作,而且确实如此.
谢谢.
遇到同样的问题,我需要使用AFFormURLParameterEncoding.
所以只是为了简化所有线程,你必须使用:
[[APIClient sharedClient] setParameterEncoding:AFFormURLParameterEncoding];
| 归档时间: |
|
| 查看次数: |
4952 次 |
| 最近记录: |