Error Domain=Nscocoaerrordomain Code=3840 "Invalid Value Around Character 0

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



Leave a reply



Submit