遠程服務器上的請求總是失敗 (Request always fail on distant server)


問題描述

遠程服務器上的請求總是失敗 (Request always fail on distant server)

I'm using the ASIHTTPRequest for my iOS application. I have to connect to a database through a PHP API (copied below). Here is my function that do the request with ASIHTTPRequest : 

-(void)startRequest {
    NSURL *url = [NSURL URLWithString:PHP_SCRIPT];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setDelegate:self];
    [request setStringEncoding:NSUTF8StringEncoding];
    [request setTimeOutSeconds:50];
    [request setPostValue:@"1" forKey:@"getWines"];
    [request startAsynchronous];
}

When I run to an API hosted localy with MAMP all works fine. But when I do it by my distant server, my request always fail, with no message.

Here is my API written in PHP, hosted on my distant server : 

<?php
 function connectToDB(){
     $connexion = mysqli_connect(HOST, USER, PASSWORD, DATABASE);
     return $connexion;
 }

 function getWines($connexion) {
     $req = "SELECT name, category FROM table WHERE prize_50  != 0 OR
                                                            prize_75  != 0 OR
                                                            prize_150 != 0 OR
                                                            prize_300 != 0";

     $res = mysqli_query($connexion, $req);

     echo '[';

     while($current = mysqli_fetch_assoc($res)) {
        extract($current);
        echo json_encode($current);
     }

     echo ']';

 }

 function getWine($connexion) {

     $name = $_REQUEST["name"];
     $req = "SELECT * FROM table WHERE name='".$name."'";


     $res = mysqli_query($connexion, $req);
     echo "[";
     echo json_encode(mysqli_fetch_assoc($res));
     echo "]";
 }

 $connexion = connectToDB();
 if($connexion) { 
    echo "connected"; 

    if(isset($_REQUEST["getWine"])) {
        getWine($connexion);
    }

    else if(isset($_REQUEST["getWines"])) {
        getWines($connexion);
    }

 }

 else {
     echo "unable to connect to the database";
 }



?>

I have no idea what happen, and why it's work on localHost and not on my distant server. If I use the API with a browser like http://myURL/iOS/objc.php?getWines=1 it's work perfectly ! 

Thank you for helping. 

P.S. I have already found a post about make some changes in the code (change the @sythesis for an handwritten getter / setter, but still not working)


參考解法

方法 1:

Your API looks to be expecting a GET request rather than a POST.  Create your URL with an NSString containing the entire URL and don't set any POST attributes.

Also, you might want to start checking out a replacement for ASI.  It isn't currently being developed and will likely never be ARC compliant.  I switched to AFNetworking and love it.

(by EdelweissLJ Wilson)

參考文件

  1. Request always fail on distant server (CC BY-SA 3.0/4.0)

#asihttprequest #iOS #web-services






相關問題

遠程服務器上的請求總是失敗 (Request always fail on distant server)

ASIHTTPRequest 緩存我的數據。我不想要任何類型的緩存 (ASIHTTPRequest caches my data. I don't want any kind of cache)

在調用之前釋放了 didFinishSelector ASIHTTPRequest 的自定義委託類 (Custom delegate class for didFinishSelector ASIHTTPRequest being deallocated before called)

Android:在後台上傳/下載文件數量的最佳方式 (Android: best way to upload/download number of files in background)

Ghi giá trị bài đăng trong ASIFormDataRequest (Log Post Value in ASIFormDataRequest)

ASIHTTPRequest - 下載問題 (ASIHTTPRequest - download problem)

ASIHTTPRequest 離線模式連接失敗 (ASIHTTPRequest offline mode connection failure)

ASIHttpRequest 請求:didReceiveBytes:未按預期工作 (ASIHttpRequest request:didReceiveBytes: not working as expected)

如何讓 Ruby on Rails 與 ASIHTTPRequest 一起使用? (How do I get Ruby on Rails to work with ASIHTTPRequest?)

可以指定一個 NSObject 作為多個 ASIHTTPRequests 的代表嗎? (OK to assign one NSObject as the delegate of multiple ASIHTTPRequests?)

需要幫助將 NSXMLParser 與 ASIHTTPRequest 一起使用 (need help using NSXMLParser with ASIHTTPRequest)

處理 ASIHTTPRequest 多個請求 (Handling ASIHTTPRequest Multiple Requests)







留言討論