gpt4 book ai didi

php - Cocoa错误3840操作无法完成

转载 作者:搜寻专家 更新时间:2023-11-01 06:49:08 25 4
gpt4 key购买 nike

我正在尝试连接到返回 json 字符串的本地主机 API(我需要与 iOS swift 应用程序一起构建)。 API 使用 Laravel 4 框架编写。

这是连接和接收代码的 iOS Swift 代码:

func checkEmail() {
var request = NSMutableURLRequest(URL: NSURL(string: "http://localhost:3306/laravel/rojectapi/checkEmail"))
var session = NSURLSession.sharedSession()

request.HTTPMethod = "POST"

var params = ["email":"myemail@me.com", "password":"password"] as Dictionary

var err: NSError?
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: &err)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")

var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
println("Response: \(response)")
var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
println("Body: \(strData)")
var err: NSError?
var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error: &err) as NSDictionary

println("hey")

if(err) {
println(err!.localizedDescription)
}
else {
var success = json["success"] as? Int
println("Success: \(success)")
}
})

task.resume()

}

Laravel PHP 路线:

Route::post('/checkEmail', array(
'as' => 'checkEmail',
'uses' => 'FrontEndController@checkEmail'
));

然后是带有 checkEmail 方法的前端 Controller :

public function checkEmail() {

$validator = Validator::make(Input::all(), array(
'email' => 'required|unique:users|email'
));

if($validator->fails()) {
return $validator->messages()->toJson();
} else {
return Response::json(array('success' => true));
}

}

我没有正确连接到服务器吗?我需要在连接到笔记本电脑的 iPhone 5s 以及模拟器中这样做。我试过name.local , localhost:<post>name.local:<post>在 URL 中。

更新

我从 this tutorial 得到代码

最佳答案

传递 NSJSONReadingAllowFragments(Swift 中的 .AllowFragments)或正确格式化您的 JSON。

简单的谷歌搜索显示错误 3840 是由于传递给 iOS JSON 解析器的 JSON 格式不正确造成的。

关于php - Cocoa错误3840操作无法完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24837820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com