1

I'm getting an error when loading JSON from a PHP output page using Swift 4.

Note: The PHP script has been checked and is working fine

let myUrl = URL(string: "http://localhost/swift/signin_up/login.php");
var request = URLRequest(url:myUrl!)
request.httpMethod = "POST"// Compose a query string
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
DispatchQueue.main.async {
    let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
        if error != nil {
            print("error=\(error!)")
            return
        }

        // You can print out response object
        print("response = \(response!)")
        print("==================================")
        //Let's convert response sent from a server side script to a NSDictionary object:
        do {
            let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary

            if let parseJSON = json {
                print(parseJSON)
            }
        } catch {
            print(" error:\(error)")
        }
    }
    task.resume()

I get this error message from Xcode:

error: Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

When I changed options to:

options: [.mutableContainers, .allowFragments]

I get this error message:

error: Error Domain=NSCocoaErrorDomain Code=3840 "Garbage at end." UserInfo={NSDebugDescription=Garbage at end.}

This is the PHP output I tested:

{"id":"1","email":"aa","password":"aa"}
7
  • When you get some error, please include the error message at least. Commented Aug 14, 2018 at 21:51
  • 1
    You can edit your own question. Commented Aug 14, 2018 at 21:58
  • I guess your php server is returning some sort of error, what do you get if you put print(String(data: data!, encoding: .utf8)) before the line let json = ... ? Commented Aug 14, 2018 at 22:38
  • i replace a code with above and get message error{error:Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.} @OOPer Commented Aug 14, 2018 at 22:49
  • print(String(data: data!, encoding: .utf8)) would never output such message and I never suggested any sort of replace. Are you reading my comment? Commented Aug 14, 2018 at 22:51

1 Answer 1

1

Be careful when you set character_set in database server choose (utf8_unicode_ci) to get correct json format when using

json_encode(//json variable);

in php

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.