user3423164
user3423164

Reputation: 221

swift error: Code=3840 "Garbage at end."

I have this error

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

here is my swift code:

var exercise:String = ""

    for value in numberOfExercisesArray{

        exercise = exercise + value.text! + ","

    }
    if exercise.characters.last == ","{

        exercise.removeAtIndex(exercise.endIndex.predecessor())

    }

the string i want to post (exercise) is in the this context : "bench press,5,100" However I cant get this string to go through because of the error above Below is how i connect to my php/sql server:

   let myURL = NSURL(string: "http://localhost:8888/saveWorkout.php");
        let request = NSMutableURLRequest(URL:myURL!);
        request.HTTPMethod = "POST";



        let postString = "exercise=\(exercise)";

        request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);

        let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
            data, response, error in

            if error != nil {
                print("error=\(error)")
                return
            }

            do{

                let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as? NSDictionary

                if let parseJSON = json{
                    let resultValue = parseJSON["status"] as! String!;

                    print("result: \(resultValue)")

                    if(resultValue == "Success"){



                        print("worked")


                    }
                    else{

                        print("not worked")
                    }
                }

            }
            catch { print(error) }

        }


        task.resume();

How do I fix this error Thanks

Upvotes: 3

Views: 5054

Answers (1)

Mr.Geeker
Mr.Geeker

Reputation: 445

Your problem is , Your are using Json , and when you try to parse json actually you are not parsing it perfectly, Json can parse as Dictionary or Array , , check your json response try to decode it in any online json parser , to see how it's look like,

  • copy your server response (json)
  • search in google ( online json formatter )
  • past it there and see

Upvotes: 1

Related Questions