2013-08-06 77 views
15

我發送POST請求到我的服務器這樣的。:AFNetworking錯誤:操作無法完成。 (可可錯誤3840)

-(Produkt*)fetchProductByID:(int)idNumber 
{ 

NSString* command = @"fetchProduct"; 
NSString* number = [NSString stringWithFormat:@"%d",idNumber]; 
NSMutableDictionary* params =[NSMutableDictionary dictionaryWithObjectsAndKeys: 
           command, @"command", 
           number, @"id", 
           nil]; 

[self commandWithParams:params onCompletion:^(NSDictionary *json) 
{ 
    //result returned 
    NSDictionary* res = [[json objectForKey:@"result"] objectAtIndex:0]; 
    NSMutableArray* dics = [[NSMutableArray alloc]init]; 

    if ([json objectForKey:@"error"]==NULL && [res count]>0) 
    { 
     //success 
     for(int i=0;i<[[json objectForKey:@"result"]count];i++) 
     { 
      res = [[json objectForKey:@"result"] objectAtIndex:i]; 
      [dics addObject:res]; 

      NSLog(@"SPECIAL FETCHED PRODUKT:%@",res); 
     } 
    } 
    else 
    { 
     //error 
     NSLog(@"error:%@",[json objectForKey:@"error"]); 
     res = [[json objectForKey:@"result"] objectAtIndex:0]; 
     NSLog(@"SPECIAL FETCHED PRODUKT:%@",res); 
    } 

}]; 

return NULL; 

} 

但我總是得到這個錯誤:錯誤:操作無法完成。 (可可錯誤3840) 的index.php文件的代碼如下所示:

<? 
require("lib.php"); 
require("QMServerAPI.php"); 

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

switch ($_POST['command']) 
{ 
    case "fetchAllProduct": //Different question 
    fetchAllProduct(); 
    break; 

    case "fetchProduct": 
    fetchProduct($_POST['id']); 
    break; 

    case "insertProduct": 
    insertProduct($_POST['name'], $_POST['artikelNr'], 
    $_POST['anleitung'], $_POST['image'], $_POST['editDate'], 
    $_POST['id']); 
    break; 

    case "updateProduct": 
    updateProduc($_POST['name'], $_POST['artikelNr'], 
    $_POST['anleitung'], $_POST['image'], $_POST['editDate'], 
    $_POST['id']); 
    break; 
} 
?> 

在這裏,我執行命令:

function errorJson($msg) 
{ 
    header('Content-type: application/json'); 
    print json_encode(array("error"=>$msg)); 
    exit(); 
} 

function fetchAllProduct() 
{ 
    //fetch all Products 
    $result = query("SELECT * FROM Produkte"); 
    print json_encode($result); 
} 

function fetchProduct($id) 
{ 
     //fetch specific product 
     $result = query("SELECT * FROM Produkte WHERE id = '122'"); 
     print jeson_encode($result); 
} 
    function insertProduct($name, $artikelNr, $anleitung, $image, 
       $editDate, $id) 
{ 

    } 

    function updateProduct($name, $artikelNr, $anleitung, $image, 
       $editDate, $id) 
    { 
    //update old product 
    } 
?> 

這裏是查詢功能的代碼:

<? 

    //setup db connection 
    $link = mysqli_connect("localhost","root",""); 
    mysqli_select_db($link, "QM"); 

    //executes a given sql query with the params and returns an array as result 
    function query() { 
    global $link; 
    $debug = false; 

    //get the sql query 
    $args = func_get_args(); 
    $sql = array_shift($args); 

    //secure the input 
    for ($i=0;$i<count($args);$i++) { 
     $args[$i] = urldecode($args[$i]); 
    $args[$i] = mysqli_real_escape_string($link, $args[$i]); 
    } 

    //build the final query 
    $sql = vsprintf($sql, $args); 

    if ($debug) print $sql; 

    //execute and fetch the results 
     $result = mysqli_query($link, $sql); 
    if (mysqli_errno($link)==0 && $result) { 

    $rows = array(); 

    if ($result!==true) 
    while ($d = mysqli_fetch_assoc($result)) { 
    array_push($rows,$d); 
    } 

    //return json 
    return array('result'=>$rows); 

} else { 

    //error 
    return array('error'=> $mysqli->error); 
} 
} 

我不知道最新的錯誤,因爲當我只選擇所有產品,它工作正常。當我使用WHERE時出錯。

感謝您的幫助。

回答

19

可可錯誤3840是一個JSON解析錯誤(如果你在Stack Overflow上搜索它,你會遇到this existing question about)。

我建議您通過JSON驗證器從您的Web服務運行您的輸出JSON,以檢查它是否實際符合規範。

+1

嘿,你是對的有幾個職位提到相同的錯誤。但似乎沒有人能解決我的錯誤。當我檢查數組時,我只是得到這個錯誤,沒有其他對象......有沒有什麼辦法來檢查服務器的響應(在mac上運行xampp,所以它是本地的)來檢查JSON輸出?正如我說的,它工作正常時,我再次使用SELECT * FROM產品 –

+0

我,我不知道我改變了什麼..我剛剛重新啓動服務器,突然一切正常,因爲它應該.... sry爲減少你的時間:( –

相關問題