Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

php

swift

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

like image 838
user3423164 Avatar asked Aug 10 '26 02:08

user3423164


1 Answers

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
like image 114
Mr.Geeker Avatar answered Aug 12 '26 14:08

Mr.Geeker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!