我没有收到服务器的任何回复,该如何解决?

时间:2018-11-24 12:49:01

标签: ios alamofire

我在一个简单的登录屏幕上就建立了网络。我使用了Alamofire和Decodable。我没有收到服务器的任何回复。为什么会这样呢? (我已将ATS代码添加到info.plist中)

import UIKit
import Alamofire

struct ResponseLogin: Decodable {
    let id: Int
    let name: String
    let surname: String
    let email: String
    let password: String
}

class ViewController: UIViewController {


@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

@IBAction func loginButtonPressed(_ sender: Any) {
    print("Button Pressed")

    guard let email = emailTextField.text else {return}
    let url = "URL"
    let body = ["email": email]

    Alamofire.request(url, method: .get, parameters: body, encoding: JSONEncoding.default).responseJSON { (response) in

        if response.result.error == nil {
            print("No Response Error")
            guard let data = response.data else { return }
            print(data)
            do {
                let responseLogin = try JSONDecoder().decode(ResponseLogin.self, from: data)
                print(responseLogin)

            } catch let jsonErr {
                print("Error serializing json:", jsonErr)
            }

        } else {
            debugPrint(response.result.error)
        }
    }

}
}

网络服务:

[WebMethod]     [ScriptMethod(ResponseFormat = ResponseFormat.Json)]     公共无效getUserDetail(字符串电子邮件)     {         userDetail用户=(从DBcontext.users中的c                            其中c.Email ==电子邮件                            选择新的userDetail()                            {                                id = c.id,                                名称= c。名称,                                姓= c。                                电子邮件= c。电子邮件,                                密码= c.Password                            })。SingleOrDefault();

    string jsonstr = new JavaScriptSerializer().Serialize(user);
    Context.Response.ContentEncoding = System.Text.Encoding.UTF8;
    Context.Response.ContentType = "application/json";
    Context.Response.Write(jsonstr);
    Context.Response.End();
}

1 个答案:

答案 0 :(得分:0)

API调用中可能存在许多问题。

首先,您可以检查您的API是否有效,请检查Postman。如果API在邮递员上运行良好,则应检入代码。

您确定后端服务器正在运行吗?