0

我必须将json格式的数据传递给服务器

{"电子邮件":"abc@gmail.com","密码":"abc"}

我已经使用了这段代码,但数据没有传递到服务器,请帮助我..

    NSDictionary *dict1=@{@"email": @"biren123@gmail.com"};
    NSDictionary *dict2=@{@"password": @"biren"};
    services *srv=[[services alloc]init];

    NSString *str=@"http://emailsending.in/setting_saver_api/";
    NSString *method=@"login.php";
    NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];

    [dict setValue:dict1 forKey:@"email"];

    [dict setValue:dict2 forKey:@"password"];


    [srv postToURL:str withMethod:method andParams:dict completion:^(BOOL success, NSDictionary *responseObj)

     {
         NSLog(@"res :%@",responseObj);
         NSLog(@"%d",success);

         NSLog(@"Successfully..................");


     }];
4

3 回答 3

1

您可以在https://www.hurl.it/查看您的链接

1)给你的http链接

2)选择您的方法类型“获取”或“发布”

3)添加参数

并检查响应。如果您得到相同的响应,请联系您的后端团队。

于 2016-02-08T11:33:48.983 回答
0

试试这个,如果这有帮助

  NSString *str = @"http://emailsending.in/setting_saver_api/"; 

NSString*method = @"login.php";

    services *srv=[[services alloc]init];

    NSMutableDictionary *dict1 = [NSMutableDictionary dictionary]; [dict1 setValue:@"biren123@gmail.com" forKey:@"email"];

    NSMutableDictionary *dict2 = [NSMutableDictionary dictionary]; [dict2 setValue:@"biren" forKey:@"password"];


    NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];

        [dict setValue:dict1 forKey:@"email"];

        [dict setValue:dict2 forKey:@"password"];

    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; manager.responseSerializer = [AFJSONResponseSerializer serializer];

    [manager POST:str parameters:dict success:^(NSURLSessionDataTask
    *task, id responseObject) {

            NSLog(@"JSON: %@", responseObject);
            //here is place for code executed in success case

    } failure:^(NSURLSessionDataTask *task, NSError *error) {

            //here is place for code executed in error case
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error while sending POST"
                                                                message:@"Sorry, try again."
                                                               delegate:nil
                                                      cancelButtonTitle:@"Ok"
                                                      otherButtonTitles:nil];
            [alertView show];

            NSLog(@"Error: %@", [error localizedDescription]); }];
于 2016-02-08T11:24:47.127 回答
0

尝试这个。1)使用 NSJSONSerialization 将您的字典转换为 json 并将其存储在 NSdata 中。

NSData * eventData = [NSJSONSerialization dataWithJSONObject: params options:0 error:&err];

2) 将包含 json 的 NSdata 发布到您的服务器

于 2016-02-06T18:30:13.267 回答