gpt4 book ai didi

ios - AF网络和发送数组错误

转载 作者:行者123 更新时间:2023-11-29 03:01:14 24 4
gpt4 key购买 nike

我正在尝试发表一篇文章,并在我的 NSDictionary 中包含一个用于发送参数的数组,但出现此错误?不知道为什么,我只能假设,因为传入数组可能会导致问题。

这是我的代码:

NSArray *categoriesSelected = [[NSUserDefaults standardUserDefaults] objectForKey:@"categories"];

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"userId": userId, @"categories": categoriesSelected};
manager.requestSerializer = [AFJSONRequestSerializer serializer]; // if response JSON format
[manager GET:@"http://kinglior.herokuapp.com/home" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
NSLog(@"Data saved");



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

错误:

数据是这样传递的:

2014-04-24 02:23:45.962 Stand[1231:60b] params being sent: {

categories = (
human,
flesh
);
userId = 535341502ac8590200e0e7b5;
}

更新了服务器端的错误:错误

    [ 'Technology', 'Fashion' ]
2014-04-25T02:09:13.950380+00:00 app[web.1]: SyntaxError: Unexpected token T
2014-04-25T02:09:13.963250+00:00 heroku[router]: at=info method=GET path=/home?categories[]=Technology&categories[]=Fashion&userId=535341502ac8590200e0e7b5 host=kinglior.herokuapp.com request_id=af7e2f9f-17f4-401d-b444-25926a4989f2 fwd="37.142.32.182" dyno=web.1 connect=0ms service=4ms status=500 bytes=937

我希望它像 ["human", "flesh"] 这样被服务器端接收...我肯定发送错误,因为我在服务器端调用 JSON.parse() 。谁能告诉我如何发送它?

更新:

这对我有用......

NSArray *categoriesSelected = [[NSUserDefaults standardUserDefaults] objectForKey:@"categories"];
NSError* error;

NSData *jsonData2 = [NSJSONSerialization dataWithJSONObject:categoriesSelected options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData2 encoding:NSUTF8StringEncoding];
NSLog(@"jsonData as string:\n%@", jsonString);

最佳答案

如果您希望请求参数以 JSON 序列化(即如果服务器期望内容类型为“application/json”),您需要使用 AFJSONRequestSerializer

manager.requestSerializer = [AFJSONRequestSerializer serializer];

关于ios - AF网络和发送数组错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23281338/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com