Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Cocoa错误3840操作无法’;不可能完成_Php_Ios_Json_Cocoa_Swift - Fatal编程技术网

Php Cocoa错误3840操作无法’;不可能完成

Php Cocoa错误3840操作无法’;不可能完成,php,ios,json,cocoa,swift,Php,Ios,Json,Cocoa,Swift,我正在尝试连接我的localhost API(需要与iOS swift应用程序一起构建),该API返回一个json字符串。该API是在Laravel4框架中编写的 以下是用于连接和接收代码的iOS Swift代码: func checkEmail() { var request = NSMutableURLRequest(URL: NSURL(string: "http://localhost:3306/laravel/rojectapi/checkEmail")) var se

我正在尝试连接我的localhost API(需要与iOS swift应用程序一起构建),该API返回一个json字符串。该API是在Laravel4框架中编写的

以下是用于连接和接收代码的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方法选择前端控制器:

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));
    }

}
我是否未正确连接到服务器?我需要在连接到笔记本电脑的iphone5s上以及在模拟器上这样做。我在URL中尝试了
name.local
localhost:
name.local:

更新


我从

获得代码,要么通过
NSJSONReadingAllowFragments
.AllowFragments
在Swift中),要么正确格式化JSON


通过简单的谷歌搜索发现,错误3840是由于将格式不正确的JSON传递给iOS JSON解析器而导致的。

如果API希望您提供一些数据,而您无法发送这些数据,或者API没有收到这些数据,则会发生这种情况,尝试回显API接收到的作为对iOS请求的响应的参数,如果API实现中存在一些错误,也可能会发生这种情况,尽管您可以在Web控制台中看到此错误。

我在project中将语言添加到本地化中时出现此错误。添加语言时,必须从“可本地化字符串”更改为“interface builder情节提要”。我是指iOS应用程序的项目。可以帮助您。

发布在调试器中登录的
strData
的内容。是的,我在发布之前也在谷歌上搜索过。但我不知道如何修复它,因为我对开发非常陌生。我如何将`NSJSONReadingAllowFragments传递给JSON?@josh就像你传递
.MutableLeaves
一样。我将
.AllowFragments
分别传递给它,而不是使用
。MutableLeaves
但它们不起作用。我还是一样error@josh那么您就有了不正确的JSON。在你的问题中发布
strData
的内容。我已经打开了charles代理服务器(监视代理调用),它显示响应是
J5.5.380.JiTE?J8ˋÄvnCDhb4{/u%Tmysql_native_password!ˍñ#08S01Got数据包顺序错误
,这将解释为什么它不读取为JSON。因为它不是JSON。但是我如何修复它?