所以我使用 Tastypie 在我的 Django 服务器上运行了这个 RESTful API。所以我正在从 javascript 中执行以下代码,一切正常。
var newData = {};
newData["content"] = "This is a new test";
$.ajax({
type: 'patch',
contentType: "application/json; charset=utf-8",
url: "/api/rest/sopsteps/17/",
data: JSON.stringify(newData),
dataType: "json",
success: function(data, status) {
alert("success");
},
error: function(data, status){
alert(data + " STATUS : " + status);
},
});
现在,如果我使用 AFNetworking/RestKit 执行以下操作,我总是会返回 401。
[26/Dec/2012 16:20:20]“补丁/api/rest/sopsteps/17/HTTP/1.1”401 0
NSDictionary* params = [[NSDictionary alloc] initWithObjectsAndKeys: _contentTextView.text, @"content", nil];
NSString *path = [NSString stringWithFormat:@"/api/rest/sopsteps/%@/",((PokaSOPStep *)_step).identifier];
[[objectManager HTTPClient]patchPath:path parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject)
{
_step.content = _contentTextView.text;
NSLog(@"%@", operation.responseString);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
if(operation.responseData)
{
id json = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
if(!json)
{
NSLog(@"An unexpected error occurred. No JSON from Server");
}
else
{
NSLog(@"An unexpected error occurred.");
}
}
else
{
NSLog(@"The Server is currently not responding.");
}
}];
但如果我从浏览器中执行此操作,我会得到:
[26/Dec/2012 16:54:57]“补丁/api/rest/sopsteps/17/HTTP/1.1”202 0
有任何想法吗?!谢谢!