上传图像时出现500内部服务器错误

时间:2013-07-09 07:38:59

标签: objective-c ios6 uiimage

我遇到了将图片上传到服务器的问题。获得500 Internal Server Error

我正在通过以下代码上传图片

ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
    [request setRequestMethod:@"PUT"];
    [request setPostValue:[[NSUserDefaults standardUserDefaults]valueForKey:@"nickname"] forKey:@"nick"];

    NSData *imageData = UIImagePNGRepresentation(img.image);
     [request addData:imageData withFileName:@"image.png" andContentType:@"image/png" forKey:@"image"];
    //[request setData:imageData withFileName:@"profileImage.jpeg" andContentType:@"image/jpeg" forKey:@"image"];

    [request setDelegate:self];
    [request setDidFinishSelector:@selector(requestSuccess:)];
    [request setDidFailSelector:@selector(requestFailed:)];
    [request startAsynchronous];

并作出回应

-(void)requestSuccess:(ASIFormDataRequest *)request{
    NSLog(@"%@",[request responseData]);
    NSError *error;

    NSString* newStr = [[[NSString alloc] initWithData:[request responseData]
                                          encoding:NSUTF8StringEncoding] autorelease];
    NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingAllowFragments error:&error];

    NSLog(@"%@",error);
}

首先将数据JSON数据转换为NSdictionary时出错,并通过以下错误

 Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x8563f70 {NSDebugDescription=Invalid value around character 0.}

然后我尝试检查是否正在转换nsstring中的Response数据,如上所述,并且通过这样做我在响应中得到字符串

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
 <title>500 Internal Server Error</title>
 <h1>Internal Server Error</h1>
 <p>The server encountered an internal error and was unable to complete your request.    Either the server is overloaded or there is an error in the application.</p>

可能是什么问题?

1 个答案:

答案 0 :(得分:0)

此:

[request setRequestMethod:@"PUT"];

应该是:

[request setRequestMethod:@"POST"];