我使用Swift将用户,密码发送到服务器,但服务器无法接收电子邮件,通过。
请告诉我,我错了。
OS X 9.5,iOS SDK 8.0,Xcode 6 beta 5
夫特:
//post data
var post:NSString = "email=\(email_merchant)&password=\(pass_merchant)&appToken=\(token_key)"
//url login
var url:NSURL=NSURL.URLWithString("http://xxx.xxx.xxx/api/sessions")
//add data
var postData:NSData=post.dataUsingEncoding(NSASCIIStringEncoding)!
var postLength:NSString = (String)(postData.length)
var request:NSMutableURLRequest=NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.HTTPBody=postData
request.setValue(postLength, forHTTPHeaderField: "Content-Length")
request.setValue("application/x-www-form-urlencode", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.setValue("d54e952e0d0586fc48f1535883ef7008", forHTTPHeaderField: "appToken")
//request.H
var reponseError: NSError?
var reponse: NSURLResponse?
var urlData: NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse: &reponse, error: &reponseError)
服务器代码:
if(empty($_POST['password']) ){
echo json_encode(array ("status"=> "empty pass")) ;
Yii::app()->end();
}
println(reponse)
0
ptional(<NSHTTPURLResponse: 0x7fea9acb1180> { URL: http://xxx.xxx.xxx/api/sessions } { status code: 200, headers {
Connection = "Keep-Alive";
"Content-Encoding" = gzip;
"Content-Length" = 43;
"Content-Type" = "text/html";
Date = "Mon, 29 Sep 2014 11:27:29 GMT";
"Keep-Alive" = "timeout=5, max=100";
Server = "Apache/2.4.7 (Ubuntu)";
Vary = "Accept-Encoding";
"X-Powered-By" = "PHP/5.5.9-1ubuntu4.3";
} })
{
status = "emmty pass";
}
感谢。
答案 0 :(得分:1)
通过Web服务访问时,无法使用普通的帖子变量访问数据。你需要像这样$data = file_get_contents("php://input");
答案 1 :(得分:1)
它是application/x-www-form-urlencoded
,而不是application/x-www-form-urlencode
。