【问题标题】:Objective C - passing parameters to webservice (cakephp) using AFNetworking 2.x libraryObjective C - 使用 AFNetworking 2.x 库将参数传递给 webservice (cakephp)
【发布时间】:2015-08-29 10:16:41
【问题描述】:

我正在尝试调用 Web 服务方法并使用 AFNetworking 2.x 将参数传递给它。我可以访问该方法(在 cakephp 中设置了正确的路由),但参数有问题。我肯定在这里遗漏了一些东西,我会感谢服务器和 obj-c 方面的任何建议。

iOS端:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"videoid": @"123", @"vote": @"123", @"udid": @"123"};

manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:@"my_url" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {

NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

NSLog(@"%@", string);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];

服务器端:

class APIController extends AppController {

public $uses = array (
    'MobileVote','Video'

);

function vote(){

$this->autoRender = false;
$response = array ();

$param = $this->request->query;
$videoid =  $this->request->query['videoid'];
$voteType =  $this->request->query['vote'];
$udid =  $this->request->query['udid'];

if(empty($udid) || empty($voteType) || empty($videoid) ){

$response['status'] = "err";
$response['msg'] = "missing parameters";
(...)

服务器响应:

Notice (8): Undefined index: videoid [APP/Controller/APIController.php, line 27]
Notice (8): Undefined index: vote [APP/Controller/APIController.php, line 28]
Notice (8): Undefined index: udid [APP/Controller/APIController.php, line 29]
{"status":"err","msg":"missing parameters"}

【问题讨论】:

  • 我对Objective-C不熟悉,但这不就是一个POST请求发送参数为application/x-www-form-urlencoded数据吗?

标签: php objective-c web-services cakephp afnetworking


【解决方案1】:

我能够解决问题。事实证明,我必须发送 GET 请求而不是 POST。

如果您在使用 API 连接时遇到问题,我强烈建议您使用此或类似的 chrome 扩展程序 - https://www.getpostman.com/

【讨论】:

    猜你喜欢
    • 2018-01-28
    • 2016-11-04
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多