How can i fix “Error Domain=NSCocoaErrorDomain Code=3840 ”No value.“ UserInfo={NSDebugDescription=No value.}”

后端 未结 1 1095
没有蜡笔的小新
没有蜡笔的小新 2020-12-17 20:51

When i run my code i get this error and i don\'t know why.

Error Domain=NSCocoaErrorDomain Code=3840 \"No value.\" UserInfo={NSDebugDescription=No value.}

I

相关标签:
1条回答
  • 2020-12-17 21:12

    You need to set the content-type header value to use JSON.

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

    Updated the code to Swift 3 and removed everything unrelated to the request:

    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()
    
    0 讨论(0)
提交回复
热议问题