Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.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
Ios 如何使用SwiftyJSON发出post请求_Ios_Swift_Http Post_Swifty Json - Fatal编程技术网

Ios 如何使用SwiftyJSON发出post请求

Ios 如何使用SwiftyJSON发出post请求,ios,swift,http-post,swifty-json,Ios,Swift,Http Post,Swifty Json,我一直在学习本教程: 并且按照每一条指示去做。但我就是无法使http post请求工作,因为我的api服务似乎无法获取请求的参数。这就是我的httppost请求方法: private func makeHTTPPostRequest(path: String, body: [String: AnyObject], onCompletion: ServiceResponse) { let request = NSMutableURLRequest(URL: NSURL(string

我一直在学习本教程: 并且按照每一条指示去做。但我就是无法使http post请求工作,因为我的api服务似乎无法获取请求的参数。这就是我的httppost请求方法:

private func makeHTTPPostRequest(path: String, body: [String: AnyObject], onCompletion: ServiceResponse) {
        let request = NSMutableURLRequest(URL: NSURL(string: path)!)

        // Set the method to POST
        request.HTTPMethod = "POST"

        do {
            // Set the POST body for the request
            let jsonBody = try NSJSONSerialization.dataWithJSONObject(body, options: .PrettyPrinted)

            request.HTTPBody = jsonBody
            let session = NSURLSession.sharedSession()

            let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
                if let jsonData = data {
                    let json:JSON = JSON(data: jsonData)

                    onCompletion(json, nil)

                } else {

                    onCompletion(nil, error)
                }

            })
            task.resume()

        } catch {
            // Create your personal error
            onCompletion(nil, nil)
        }
    }
然后我创建了一个函数来调用它:

func registerNewUser(email: String,url: String, onCompletion: (JSON) -> Void) {

        let route = baseURL + url

        print(route)

        let postItems:[String: String] = ["email": email]
        print("postitems",postItems)



        makeHTTPPostRequest(route, body: postItems, onCompletion: { json, err in

            onCompletion(json as JSON)

            print("response",json)

        })

    }
然后从注册控制器调用它:

func requestSignUp()
    {
        if((emailField.text?.isEmpty) == false)
        {
        //performSegueWithIdentifier("showVerification", sender: signUpBtn)

            let url = "user/signup"
            RestApiManager.sharedInstance.registerNewUser(emailField.text! as String, url: url as String,  onCompletion: {
                (json: JSON) in if let results = json[0].array {
                    for entry in results {

                        print(entry)

                    }
                }
            })

        }
    }
打印内容将“注册用户”下的帖子格式显示为:

positems[“电子邮件”:“dasdsd”]

我的服务响应显示:

response {
  "responseData" : {

  },
  "responseStat" : {
    "status" : false,
    "isLogin" : false,
    "requestError" : [

    ],
    "msg" : "The email field is required.",
    "extra" : {

    }
  }
}
email字段参数为email,成功提交后会显示如下内容:

{
  "responseStat": {
    "status": true,
    "isLogin": false,
    "msg": "Successfully Registered an, your password has been sent to your email",
    "requestError": [],
    "extra": {}
  },
  "responseData": ""
}

在mymakeHTTPPostRequest中添加此行使其正常工作:

request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
您可以轻松使用:

static func requestToSendObject(value: YOUROBJECT) {
    let url = NSURL(string: "http://google.com")!
    let params: [String: AnyObject] = [
        "object_key" : value
    ]
    Alamofire.request(.POST, url, parameters: params, encoding: .JSON, headers: nil).responseJSON {
        if let data = response.result.value {
            let responseJSON = JSON(data)
            // Use the responseJSON
        }
    }
}