douzhuan0309 2016-03-10 04:09
浏览 71

Swift 2,解析JSON,空响应

I recently made a single-screen web-based application using PHP and AJAX. I want to port this app to iOS, and have started to teach myself Swift. I had the application working perfectly in Swift 1, but since I updated to xCode 7.2 and Swift 2 so that I could try the app on my iPhone with iOS 9, the code was completely non-functional. From here, I have new code now, and as far as I can see, it should work, but I am getting an empty response. What is wrong?

import UIKit
import Foundation

class LoginViewController: UIViewController, UITextFieldDelegate {



    @IBOutlet weak var username_textField: UITextField!

    @IBOutlet weak var password_TextFieldOutlet: UITextField!


    @IBAction func password_TextFieldAction(sender: AnyObject) {


        let username = username_textField.text
        let password = password_TextFieldOutlet.text
        var urlRequest = "~~URL~~/login.php?username=\(username)&password=\(password)"



    }


    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

The idea is I was to get my data from the database and see if "success" (a boolean value in the db) returns 1 or 0. If it returns 1, then perform a segue to next page, if not, create an alert saying incorrect username and password.

Thank you in advance!

  • 写回答

2条回答 默认 最新

  • duanbishai5271 2016-03-10 04:29
    关注
    1. Create the url

      let url = NSURL(string: "http://google.com")

    2. Create the request

      let request = NSMutableUrlRequest(url: url!)

    3. Add data and request methods

      request.HTTPMethod = "POST" request.HTTPBody = "username=\(username text.text!)".dataUsingEncoding(NSUTF8StringEncoding)

    4. Send the connection

        NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { 
           (response, data, error) -> Void in
       if data != nil && error == nil {
           if let response = String(data: data!, encoding: NSUTF8StringEncoding) { 
                //here is where you now have your response 
           }
       })
      
    评论

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?