swift
1import UIKit 2 3class ViewController: UIViewController { 4 5 @IBOutlet weak var name: UITextField! 6 @IBOutlet weak var password: UITextField! 7 @IBOutlet weak var label1: UILabel! 8 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 // Do any additional setup after loading the view, typically from a nib. 12 13 name.attributedPlaceholder = NSAttributedString(string:"name", 14 attributes:[NSForegroundColorAttributeName: UIColor.whiteColor()]) 15 16 name.layer.borderWidth = 1.0 17 name.layer.borderColor = UIColor.whiteColor().CGColor 18 19 password.attributedPlaceholder = NSAttributedString(string:"password", 20 attributes:[NSForegroundColorAttributeName: UIColor.whiteColor()]) 21 22 password.layer.borderWidth = 1.0 23 password.layer.borderColor = UIColor.whiteColor().CGColor 24 25 } 26 27 override func didReceiveMemoryWarning() { 28 super.didReceiveMemoryWarning() 29 // Dispose of any resources that can be recreated. 30 } 31 32 @IBAction func login(sender: AnyObject) { 33 34 // 名前取得 35 let nameContent = name.text! 36 37 // パスワード取得 38 let passwordContent = password.text! 39 40 // json形式 41 let post = "{" + "\"nameContent: \"\(nameContent)\"," + "\"passwordContent\": \"\(passwordContent)\"" + "}" 42 43 // dataをUTF-8でエンコード 44 let postdata = post.dataUsingEncoding(NSUTF8StringEncoding)! 45 46 // POST先のURLを生成 47 let url = NSURL(string: "ここに自分のURL") 48 49 // HTTPサーバーに接続するためのクラスをインスタンス 50 let req = NSMutableURLRequest(URL: url!) 51 52 // メソッドをPOSTに設定 53 req.HTTPMethod = "POST" 54 55 // HTTPヘッダを追加 第1引数 値(今回はjsonデータを送るのでjson) 第2引数 フィールド名 56 req.setValue("application/json", forHTTPHeaderField: "Content-Type") 57 58 // コンテンツの長さを取得 59 let length = postdata.length 60 61 // HTTPヘッダを追加 62 req.setValue("\(length)", forHTTPHeaderField: "Content-Length") 63 64 // bodyを追加 65 req.HTTPBody = postdata 66 67 let task = NSURLSession.sharedSession().dataTaskWithRequest(req,completionHandler: { 68 ( data, res, err ) in 69 if data != nil { 70 let text = NSString(data: data!, encoding: NSUTF8StringEncoding) 71 dispatch_async(dispatch_get_main_queue(), { 72 self.label1.text = text as String? 73 }) 74 } else { 75 dispatch_async(dispatch_get_main_queue(), { 76 self.label1.text = "error" 77 }) 78 } 79 80 }) 81 82 task.resume() 83 84 } 85 86 87} 88
php
1<?php 2 3 $post = file_get_contents("php://input"); 4 $json = json_decode($post); 5 6 var_dump($json); 7
なぜかlabel1にerrorが表示されちゃいます。
本当は入力してるものがlabel1に表示されるんですが謎です
あなたの回答
tips
プレビュー