我在应用程序中使用sthttprequest和php进行and服务器通信。这是一个示例请求:
-(void)GetSomething:(NSString*)Username :(NSString*)Password {
__block STHTTPRequest *up = [STHTTPRequest requestWithURLString:[NSString stringWithFormat:@"%@%@",PATHSERVER,@"getsomething.php"]];
up.POSTDictionary = @{@"username":Username,@"password":Password};
up.completionBlock = ^(NSDictionary *headers, NSString *body) {
//my result data
};
up.errorBlock = ^(NSError *error) {
NSLog(@"-error description and error code- %@", [error localizedDescription]);
};
[up startAsynchronous];
}当发生条件时,我希望在我的php页面中输出一个错误,以便在sthttprequest错误块中显示一个警报:
up.errorBlock = ^(NSError *error) {
NSLog(@"-error description and error code- %@", [error localizedDescription]);
};可以从php页面强制自定义代码错误来实现这种行为?类似于:
<?php echo $custom_error_code; ?>
up.errorBlock = ^(NSError *error) {
if (error.code==1300){
//this is the code i have set in php
}
}; 我不想要一个像503,500这样的错误,等等.因为我有风险在没有意义的时候向用户显示警报。出于实际原因,我希望使用块错误,而不是completionBlock中的简单字符串。
编辑
我在堆栈溢出Can we create custom HTTP Status codes?中找到了这个答案
因此,我可以创建自定义错误代码,在没有分配数字的情况下,我必须尊重的是类号4xx或5xx。现在,我的问题是从php页面输出像"475“这样的自定义错误代码。该方法
header_status(475);不是为我工作。
发布于 2015-02-20 08:22:00
这个答案https://stackoverflow.com/a/23190950/3057259给了我正确的方法将一个自定义错误代码发布到正确识别它的sthttprequest。
https://stackoverflow.com/questions/28612105
复制相似问题