0

嗨,我正在尝试发出 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];

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

4

4 回答 4

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];

可能对你有帮助。

于 2014-09-08T12:48:55.103 回答
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 
{


}
于 2014-09-08T11:59:42.507 回答
0

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

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

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



所以而不是:

{"status":"success"}

您最有可能尝试解析:

<html>
  <head></head>
  <body>{"status":"success"}</body>
</html>
于 2014-09-08T12:26:34.923 回答
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 );

}
于 2015-06-01T14:48:22.280 回答