0

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

何か案は?!ありがとう!

4

1 に答える 1

0

401 は「無許可」の HTTP コードです。ブラウザと AFNetworking で異なる結果が得られるのは、前者にログインしていて、後者にはログインしていないためです (別のブラウザでこれを試すことで確認できます)。

サーバーにどのような種類の認証方式を設定したかはわかりませんが、その要求を行う前に iOS でログインする必要があります。

于 2012-12-28T15:06:57.480 に答える