尝试请求POST方法时,ASIFormDataRequest返回NULL

时间:2014-09-08 11:51:53

标签: ios post asihttprequest asiformdatarequest

您好我正在尝试发出POST请求

我的代码:

NSURL *url = [NSURL URLWithString:urlString];
    __weak ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setDelegate:self];
    [request setRequestMethod:@"POST"];
    [request setPostValue:@"JustinBieber" forKey:@"fname"];

    [request setCompletionBlock:^{
        NSString *result = [request responseString];
        NSDictionary *dict = [result JSON];
        NSLog(@"dict -%@",dict);
    }];
    [request setFailedBlock:^{
        NSLog(@"error %@",[request error]);

    }];
    [request startAsynchronous];

当我运行我的代码时,它返回一个(null)值。我的urlString是正确的,请求也没有给我错误。我已经在网上尝试了它并返回{"状态":"成功"}(它将返回状态成功或失败的字典)。

4 个答案:

答案 0 :(得分:0)

像这样使用。希望这会有所帮助。

-(void)exe method
 {
     NSString *strURL=@"---your URL----";
     NSURL *url=[NSURL URLWithString:strURL];
     ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url];
     [request setRequestMethod:@"POST"];
     [request setPostValue:@"JustinBieber" forKey:@"fname"];
     [request setDelegate:self];
     [request setTimeOutSeconds:60];
     [request startAsynchronous];
}


- (void)requestFinished:(ASIHTTPRequest *)request
{

    NSError *error;
    if(!error)
    {

        NSString *receivedString = [request responseString];
        NSDictionary *dic = [receivedString JSONValue];
       NSLog(@"output %@",dic);

     }


}
- (void)requestFailed:(ASIHTTPRequest *)request 
{


}

答案 1 :(得分:0)

您是否在正确返回JSON的脚本中设置了标头?假设你正在使用PHP:

header('Content-Type: application/json');

默认的MIME类型是" text / plain",而不是" application / json"。如果您没有正确设置MIME类型,则基本上尝试解析整个文档。



所以而不是:

{"status":"success"}

您最有可能尝试解析:

<html>
  <head></head>
  <body>{"status":"success"}</body>
</html>

答案 2 :(得分:0)

尝试使用此代码 -

NSURL *url = [NSURL URLWithString:urlString];
__unsafe_unretained ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:@"POST"];
[request setPostValue:@"JustinBieber" forKey:@"fname"];
[request setDelegate:self];

__block id jsonData;
[request setTimeOutSeconds:300];
[request setCompletionBlock:^(){
    NSError *error = nil;
    NSString *responseString = (request.responseString.length)?request.responseString:@"";
    NSLog(@"%@",responseString);
    NSData *responseData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
    jsonData = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
    if(error)
        completionBlock(nil, error, task);
    else
        completionBlock(jsonData, error, task);
}];

[request setFailedBlock:^{
    completionBlock(nil, request.error, task);
}];
[request startAsynchronous];

可能会对你有所帮助。

答案 3 :(得分:0)

-(void)exe method
 {
     NSString *strURL=@"---your URL----";
     ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strURL]];
     [request setDelegate:self];
     [request setRequestMethod:@"POST"];
     [request setPostValue:@"JustinBieber" forKey:@"fname"];
     [request setTimeOutSeconds:60];
     [request startAsynchronous];
}


- (void)requestFinished:(ASIHTTPRequest *)request
{
        NSString *receivedString = [request responseString];
       NSLog(@"output %@",receivedString );

}
- (void)requestFailed:(ASIHTTPRequest *)request 
{
       NSString *receivedString = [request responseString];
       NSLog(@"output %@",receivedString );

}