2015-04-24 24 views
0

我的迴應顯示爲空。但是當你在瀏覽器中輸入網址時,它會顯示1.但是在我的代碼中,它返回0. 網址是http://boomagift.ramansingla.com/[email protected]。如果有人能幫助我,我會很感激。我是iOS新手。迴應爲空

+(NSDictionary *)forgotpassword:(NSString *)email 
{ 
    NSDictionary *dict=[[NSDictionary alloc]init]; 


    NSString *urlStr=[NSString stringWithFormat:@"http://boomagift.ramansingla.com/forgotpassword.php?email=%@",email]; 
    NSLog(@"%@",urlStr); 

    NSMutableURLRequest *request=[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlStr]]; 
    dict = [self sendRequest:request]; 
    NSLog(@"%@",dict); 

    return dict; 
} 

+(NSDictionary *)sendRequest:(NSMutableURLRequest *)request 
{ 
    NSHTTPURLResponse *response; 
    NSError *error; 
    NSData *responseData; 
    responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
    if(responseData&&[responseData length]) 
    { 

     NSDictionary *dictionary=[NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error]; 
     return dictionary; 
    } 
    else 
    { 
     UIAlertView *noInternetAlert=[[UIAlertView alloc] initWithTitle:@"Boom A GIft" message:@"Server Error" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [noInternetAlert show]; 
     noInternetAlert=nil; 
     return nil; 
    } 
} 
+0

如果我在瀏覽器中運行這個URL我得到了答案** 1 **,它是正確的或你得到的迴應是別的 –

+0

是的即時獲得響應是null在我的代碼和瀏覽器中顯示1 – user3651677

+0

在瀏覽器中它是0請檢查 – aBilal17

回答

1

響應數據是無效的JSON,因爲它的頂部項目是一個數字,而不是一個集合(列表或對象)。

An object that may be converted to JSON must have the following properties:

  • The top level object is an NSArray or NSDictionary .

  • All objects are instances of NSString , NSNumber , NSArray , NSDictionary , or NSNull .

  • All dictionary keys are instances of NSString .

  • Numbers are not NaN or infinity.

您可以將序列化程序配置爲接受options參數的頂級非收集對象。

enum { 
    NSJSONReadingMutableContainers = (1UL << 0), 
    NSJSONReadingMutableLeaves = (1UL << 1), 
    NSJSONReadingAllowFragments = (1UL << 2) 
}; 
typedef NSUInteger NSJSONReadingOptions; 

NSJSONReadingAllowFragments

Specifies that the parser should allow top-level objects that are not an instance of NSArray or NSDictionary .

Available in OS X v10.7 and later.

BTW:一個錯誤輸出參數不是在開玩笑返回nil(不NULL),但一個提示,是什麼問題。 ;-)

+0

我們如何做到這一點.. – user3651677

+0

增加更多文字 –

+0

我們怎麼過來這個。我在代碼 – user3651677