我该如何解决“错误域= NSCocoaErrorDomain代码= 3840”没有值“ UserInfo = {NSDebugDescription =没有值}”

问题描述

您需要将content-type标头值设置为使用JSON。

request.addValue("application/json", forHTTPHeaderField: "Content-Type")

将代码更新为Swift 3,并删除了与请求无关的所有内容:

let myUrl = URL(string: "http://foodhelper.club/registerUser.php");
var request = URLRequest(url:myUrl!);
request.httpMethod = "POST";
request.addValue("application/json", forHTTPHeaderField: "Content-Type")

let postString = "userEmail=email&userFirstName=firstname&userLastName=lastname&userPassword=password";
request.httpBody = postString.data(using: String.Encoding.utf8);

URLSession.shared.dataTask(with: request, completionHandler: { (data:Data?, response:URLResponse?, error:Error?) -> Void in
        if error != nil {
            print("fail")
            return
        }

        do {
            let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
            print ("1")

            if let parseJSON = json {

                let userId = parseJSON["userId"] as? String
                print ("2")
                if( userId != nil) {

                } else {
                    let errorMessage = parseJSON["message"] as? String
                    print ("3")
                }
            }
        } catch{
            print(error)
        }
}).resume()

解决方法

当我运行代码时,出现此错误,我不知道为什么。

错误域= NSCocoaErrorDomain代码= 3840“无值。” UserInfo = {NSDebugDescription =无值。}

我在互联网上寻找它,但没有找到任何东西。

这是我的代码:

 let myUrl = NSURL(string: "http://foodhelper.club/registerUser.php");
    let request = NSMutableURLRequest(URL:myUrl!);
    request.HTTPMethod = "POST";

    let postString = "userEmail=\(userEmail!)&userFirstName=\(userFirstName!)&userLastName=\(userLastName!)&userPassword=\(userPassword!)";

    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);

    NSURLSession.sharedSession().dataTaskWithRequest(request,completionHandler: { (data:NSData?,response:NSURLResponse?,error:NSError?) -> Void in

        dispatch_async(dispatch_get_main_queue())
        {



            if error != nil {
                self.alertMessage(error!.localizedDescription)
                print("fail")
                return
            }

            do {
            let json = try NSJSONSerialization.JSONObjectWithData(data!,options: .MutableContainers) as? NSDictionary
                print ("1")
              if let parseJSON = json {

                    let userId = parseJSON["userId"] as? String
                    print ("2")
                    if( userId != nil)
                    {
                        let myAlert = UIAlertController(title: "Alert",message: "Registration successful",preferredStyle: UIAlertControllerStyle.Alert);

                        let okAction = UIAlertAction(title: "OK",style: UIAlertActionStyle.Default){(action) in

                            self.navigationController?.popViewControllerAnimated(true)                            }

                        myAlert.addAction(okAction);
                        self.presentViewController(myAlert,animated: true,completion: nil)
                    } else {
                        let errorMessage = parseJSON["message"] as? String
                        print ("3")
                        if(errorMessage != nil)
                        {
                            self.alertMessage(errorMessage!)
                        }

                    }

                }
            } catch{

                //email vergleich fehlt,egal ob
                print(error)
                print("catched error")

                let myAlert = UIAlertController(title: "Alert",preferredStyle: UIAlertControllerStyle.Alert);

                let okAction = UIAlertAction(title: "OK",style: UIAlertActionStyle.Default){(action) in


                    self.navigationController?.popViewControllerAnimated(true)
                }

                myAlert.addAction(okAction);
                self.presentViewController(myAlert,completion: nil)


            }



        }

    }).resume()


}

谢谢您的帮助

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...