Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 西铁休息服务;使用NSError处理iOS错误_Jquery_Asp.net_Ios_Wcf_Rest - Fatal编程技术网

Jquery 西铁休息服务;使用NSError处理iOS错误

Jquery 西铁休息服务;使用NSError处理iOS错误,jquery,asp.net,ios,wcf,rest,Jquery,Asp.net,Ios,Wcf,Rest,我有一个WCF RESTful服务,并试图勾勒出我将如何处理服务器和各种客户端上的错误。可以从web(jQuery)和iOS产品访问该服务。下面看一下我是如何在服务上抛出错误的: [WebGet(UriTemplate = "get/{id}", ResponseFormat = WebMessageFormat.Json)] public Person Get(string id) { //check security if(!Secur

我有一个WCF RESTful服务,并试图勾勒出我将如何处理服务器和各种客户端上的错误。可以从web(jQuery)和iOS产品访问该服务。下面看一下我是如何在服务上抛出错误的:

    [WebGet(UriTemplate = "get/{id}", ResponseFormat = WebMessageFormat.Json)]
    public Person Get(string id)
    {
        //check security
        if(!SecurityHelper.IsAuthenticated()) { throw new WebFaultException<PersonException>(new PersonException { Reason = "Permission denied." }, HttpStatusCode.Unauthorized); }
…一切都很好-调用完成并抛出错误(因为我没有提供任何身份验证),然后调用错误:callback。在检查xhr.responseText时的错误回调中,我得到了正确的JSON对象({“reason”:“Permission denied!”}),显示了服务器提供的错误原因

现在-我正在尝试整合我的iOS应用程序来调用同一个服务,除了无法获得该服务提供的错误详细信息外,一切都很正常。以下是我从iOS调用REST服务时的代码:

//set up for any errors
NSError *error = nil;

//set up response
NSURLResponse *response = [[NSURLResponse alloc] init];

//make the request
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

//check for error
if(error)
{
    //debug
    NSLog(error.description);

    //send back error
    return error;
}
else 
{
在error.description中,我只收到一条一般消息,如“操作无法完成”

如何获取服务器发送的自定义错误信息?我一直在查看NSError类的userInfo属性,但无法确定是否可以获取自定义信息,如果可以,我将如何进行


提前感谢您的帮助。

错误消息将显示在请求返回的数据上(响应正文):


错误消息将出现在请求(响应主体)返回的数据上:


是的,刚刚检查过,确实是。非常感谢…我很感激。是的,我刚检查过,确实如此。非常感谢…我很感激。
//set up for any errors
NSError *error = nil;

//set up response
NSURLResponse *response = [[NSURLResponse alloc] init];

//make the request
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

//check for error
if(error)
{
    //debug
    NSLog(error.description);

    //send back error
    return error;
}
else 
{
//make the request
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

if (error) {
    if (data) {
        NSString *respBody = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    } else {
        NSLog(@"%@", error.description);
    }
}
else 
{
    // get response
}