Error (Error Domain=NSCocoaErrorDomain Code=3840 Invalid value around character 0. UserInfo={NSDebugDescription=Invalid value around character 0.}
There is an issue with my web service. They are giving me the response in "text/HTML" format rather than HTML. When i printed my response on debugger then i got:
"Content-Type" = "text/html; charset=UTF-8";
Now, i updated my webservice and everything is working like a charm.
swift error Domain=NSCocoaErrorDomain Code=3840 Invalid value around character 1.
Adding .json at the end of the URL endpoint has solved the error. ie https://www.example.com/rest/user/login.json
Alamofire post request getting **Invalid value around line 1, column 0.**
Below code is working when i removed headers from request.
func apiPostRequest1(parameters:[String:String], url:String, completionHandler: @escaping (Any?) -> Swift.Void) {
session.request(url,
method: .post,
parameters: parameters,
encoding: URLEncoding.httpBody).validate(statusCode: 200..<600).responseJSON{ response in
switch response.result {
case .success(let JSON):
completionHandler(JSON)
case .failure(let error):
let responseData = String(data: response.data!, encoding: String.Encoding.utf8)
print(responseData ?? "Error in encoding response data")
print("Request failed with error \(error)")
completionHandler(response.response?.statusCode)
}
}
}
Error Domain=NSCocoaErrorDomain Code=3840 Invalid value around character 0. UserInfo={NSDebugDescription=Invalid value around character 0
First, remove text "Successfully connect" in your PHP code. And the result is not in json format so far. Try to improve it.
Second , at iOS side, you directly use url string as JSON. The right way is to get data from the url, and then convert it to JSON. Try:
NSError *error = nil;
NSString *url_string = @"http://127.0.0.1/RetriveData.php";
NSURL *url = [NSURL URLWithString:url_string];
if (url != nil) {
NSData *data = [NSData dataWithContentsOfURL:url];
NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
NSLog(@"%@",error);
NSLog(@"json: %@", json);
}
Related Topics
Scenekit Ar Game Fps Getting Low and The Device Getting Hot with Use
Shortest Code to Create an Array of Random Numbers in Swift
How to Upload Images from The Browser to Amazon S3 Using Vapor and Leaf
Using Auto Layout to Orientate Stack Views Vertically in Portrait and Horizontally in Landscape
How to Use This Fetchrequest() in Swift
Automatically Reload Tableviewcontroller on Rewind
Sdwebimage, Swift: Sdwebimagerefreshcached Unresolved Identifier
Parse Weird Bug in Swift That Causes Acl Write Permissions to Change to an Objectid
Trouble Calling a Method in an Init
How to Do Imageview.Startanimating() with Completion in Swift
Swift: Trunc a Floating Number to Show It in a Label
How to Detect Day Change in Swift
Is There an Firebase Realtime Database Equivalent to The Increment Method Available in Firestore
Swiftui Go Back Programmatically from Representable to View
Actions Assigned to Nsmenuitem Dont Seem to Work
Extension for Average to Return Double from Numeric Generic
Testing a Class Which Preserves Its State in Private Variables