Tôi gặp sự cố với AFNetworking. Tôi không thể đặt content-type
thành application/json
. Làm thế nào tôi có thể làm được? tôi thử rất nhiều giải pháp, nhưng không ai làm việc cả. sau đây là mã của tôi:Cách đặt loại nội dung trong AFnetworking
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
if (self.enableBasicAuth){
[manager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:[[SOWMerchant merchant] username] password:[[SOWMerchant merchant] password]];
}
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:[NSString stringWithFormat:@"%@", url] parameters:postData success:^(AFHTTPRequestOperation *operation, id responseObject) {
[self onSuccessResponse:responseObject request:operation];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self onError:error request:operation];
}];
tôi nhận được phản ứng này
{ URL: https://sbengtest.spotonway.com:9099/v1.0/merchant/check }, response: <NSHTTPURLResponse: 0x10aa20530> { URL: https://sbengtest.spotonway.com:9099/v1.0/merchant/check } { status code: 400, headers {
Connection = "keep-alive";
"Content-Length" = 192;
"Content-Type" = "text/html";
Date = "Sun, 20 Jul 2014 22:34:59 GMT";
Server = "nginx/1.2.1";
} }>
UPDATE:
i trye thêm
AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
operationManagerInstance.requestSerializer = requestSerializer;
tai nạn ứng dụng của tôi
CẬP NHẬT này 2: nó hoạt động. tôi đã xóa dòng này
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
Bạn đã thử sử dụng 'AFJSONRequestSerializer' nó sẽ tự động thiết lập kiểu nội dung thích hợp? – blazejmar
có, nó không hoạt động – crox
Vui lòng đăng nội dung phản hồi và tất cả các tiêu đề yêu cầu bạn đang gửi. Có thể có vấn đề với tiêu đề chấp nhận không phải là loại nội dung. – blazejmar