Inside dataTaskWithRequest is not Executing. In Swift While Hitting the API

后端 未结 1 1587
醉酒成梦
醉酒成梦 2021-01-17 06:47

Same Data when I am hitting with POSTMAN I m getting the desired response. But in my Function inside dataTaskWithRequest I am Not Getting Any Response.

相关标签:
1条回答
  • 2021-01-17 07:44

    Posting code working on my side based on our discussion:

    //MARK: URLRequest
    
    func sendData (){
    
        var dict = [String:AnyObject]()
    
        dict =
        [
            "data" : [
                    [
                        "answer" : [9353091],
                        "question" : 31675931
                    ],
                    [
                        "answer" : [9353101],
                        "question"  : 31675936
                    ]
            ],
            "end" : 5565665,
            "quizId" : 1206500,
            "start" : 5565656
        ]
    
        print(dict)
    
        let url = NSURL(string: "http://www.proprofs.com/quiz-school/mobileData/request.php?request=QuizTotal&module=PQ")!
        let request = NSMutableURLRequest(URL: url)
        request.HTTPMethod = "POST"
        request.HTTPBody = try!  NSJSONSerialization.dataWithJSONObject(dict, options: NSJSONWritingOptions())
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        let task =  NSURLSession.sharedSession().dataTaskWithRequest(request){ data, response, error in
            if(error != nil){
                print("ERRORRRRRRR : \(error)")
                return
            }
    
            print("RESPONSEEEEE : \(response)")
    
            do {
                let dic = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String:AnyObject]
    
                print("DIIIIIIIC \(dic)")
            }catch {
                print("adsfdasfdsfdsafdsfadsfs")
            }
    
            ((response as! NSHTTPURLResponse).statusCode)
        }
        task.resume()
    }
    

    Final Edit:

    The OP did not had issue in the above mentioned code. After hours of discussion and chat with him I saw that the error was in the didSelectRowAtIndexPath, the project was crashing immediately after the sendData: method was called, hence not response was received from the server.

    0 讨论(0)
提交回复
热议问题