我正在swift中构建一个应用程序,后端在Laravel中(所以我们也可以移植到android)。 laravel应用程序很好地连接到数据库(它在一周前工作)然后我们将api url更改为在线服务器,我现在需要将其更改回我的localhost以测试更多的后端开发。
但现在从服务器获取数据时,它说:
J���
5.5.38�Ù���3FEgW_Vp�ˇ˜�Ä����������T4E?L@:AwU&X�mysql_native_password�!��ˇÑ#08S01Got packets out of order
以下是laravel中的createUser函数的代码:
public function createUser() {
$validator = Validator::make(Input::all(), array(
'username' => 'required|min:1|unique:users',
'password' => 'required|min:1',
'email' => 'required|unique:users|email'
));
if($validator->fails()) {
// return $validator->messages()->toJson();
return Response::json(array('success' => false, 'error_message' => 'That email or password is already in use'));
} else {
$user = User::create(array(
'username' => Input::get('username'),
'password' => password_hash(Input::get('password'), PASSWORD_BCRYPT),
'email' => Input::get('email'),
'type' => 'business',
'active' => 1
));
if($user) {
$user2 = DB::table('users')->where('email', Input::get('email'))->first();
return Response::json(array('success' => true , 'userid' => $user2->userid));
} else {
return Response::json(array('success' => false, 'error_message' => 'The user could not be created'));
}
}
}
这里是swift中的代码(它适用于以JSON响应的在线服务器):
if ( username.isEqualToString("") || password.isEqualToString("") || email.isEqualToString("")) {
var alertView:UIAlertView = UIAlertView()
alertView.title = "Sign Up Failed!"
alertView.message = "Please enter Email, Username and Password"
alertView.delegate = self
alertView.addButtonWithTitle("OK")
alertView.show()
} else {
var post:NSString = "username=\(username)&password=\(password)&email=\(email)"
NSLog("PostData: %@",post);
var url:NSURL = NSURL.URLWithString("http://localhost:3306/laravel/project/createUser/")
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-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
var error: NSError?
var response: NSURLResponse?
var urlData:NSData = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&error)
let res = response as NSHTTPURLResponse!;
NSLog("Response code: %ld", res.statusCode);
if (res.statusCode >= 200 && res.statusCode < 300)
{
var responseData:NSString = NSString(data:urlData, encoding:NSUTF8StringEncoding)
NSLog("Response ==> %@", responseData);
var error: NSError?
let jsonData:NSDictionary = NSJSONSerialization.JSONObjectWithData(urlData, options:NSJSONReadingOptions.MutableContainers , error: &error) as NSDictionary
let success = jsonData.valueForKey("success").integerValue
//[jsonData[@"success"] integerValue];
NSLog("Success: %ld", success);
if(success == 1)
{
NSLog("Register SUCCESS");
self.dismissViewControllerAnimated(true, completion: nil)
} else {
var error_msg:NSString
if jsonData["error_message"] as? NSString {
error_msg = jsonData["error_message"] as NSString
} else {
error_msg = "Unknown Error"
}
var alertView:UIAlertView = UIAlertView()
alertView.title = "Register Failed!"
alertView.message = error_msg
alertView.delegate = self
alertView.addButtonWithTitle("OK")
alertView.show()
}
} else {
var alertView:UIAlertView = UIAlertView()
alertView.title = "Register Failed!"
alertView.message = "Connection Failed"
alertView.delegate = self
alertView.addButtonWithTitle("OK")
alertView.show()
}
}
数据库是localhost:3306
上MySQL Workbench的帮助。在Swift代码中,我还尝试了myname.local/
和localhost
。
非常感谢任何帮助