看板 MacDev 關於我們 聯絡資訊
我想用POST的方式將資料以json格式傳送到php,接受後decode json 然後UPDATE mysql... 但是不管我怎麼試就是沒動靜 看了很多網頁教學也都一樣找不到問題.. 希望有人可以幫我找到問題,拜託了!! 這是我的程式碼 NSURL *objUrl = [NSURL URLWithString : @"http://localhost/liors~/postdict.php"] ; NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL : objUrl] ; [theRequest setHTTPMethod : @"POST"]; [theRequest setValue : @"application/json" forHTTPHeaderField : @"Content-Type"]; [theRequest setTimeoutInterval:10]; NSString *stringData = [[NSString alloc] initWithString:@"BOSS"]; NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObject:stringData forKey:@"Name"]; NSString *jsonString = [jsonDictionary JSONRepresentation]; NSString *strHttpBody = [NSString stringWithFormat:@"%@", jsonString]; [theRequest setHTTPBody:[strHttpBody dataUsingEncoding:NSUTF8StringEncoding]]; [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; php的部分 <?php $rawJsonData = $_POST['json']; $decodedData = json_decode($rawJsonData); //Connect To Database $hostname='**BLACKEDOUT**.com'; $username='**BLACKEDOUT**'; $password='**BLACKEDOUT**'; $dbname='**BLACKEDOUT**'; $usertable='users'; mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.'); mysql_select_db($dbname); $query = "UPDATE $usertable SET Name = '$decodedData' ";//do i encode? serialize? dunno $result = mysql_query($query); ?> -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.47.134.69
LouisXIV:我猜 [NSString stringWithFormat:@"jason=%@" <== 07/25 15:31
tentenlee:NSString *jsonString = [jsonDictionary JSONRepresent 07/25 15:41
tentenlee:可以這樣做? 需要匯入甚麼東西嗎? 07/25 15:41
chichi30:謝謝!!可以了!! 07/25 19:40
chichi30:回2F 好像要去匯入SBJSON喔!! 07/25 19:42