I am using AFJSONRequestOperation
to request a server and parse the returned JSON response, but while parsing, I got this error:
NSDebugDescription = "JSON text did not start with array or object and option to allow fragments not set.";
I checked the API and it's returning JSON data:
header('Content-type: text/json');
$arr[] = array("Message" => "update succeeded");
echo '{"Result":'.json_encode($arr).'}';
Any idea how to fix that?
EDIT
I tried to call the API from browser and include the request in the url, and so I got a valid JSON response:
{"Result":[{"Message":"update succeeded"}]}
First thing, json_encode
the entire object rather than breaking into it pieces.
Secondly, unless $arr contains multiple elements (not clear from example above), it should be initalized as thus:
$arr = array("Message" => "update succeeded");
I'm still not certain what else could be the issue here. You should echo out what your app is receiving and that should indicate the issue.
Please use acceptable content type. in your webservice that should be only plain text.
here is my swift code and fixed:
let manager = AFHTTPRequestOperationManager()
manager.requestSerializer=AFJSONRequestSerializer()
manager.responseSerializer = AFHTTPResponseSerializer();
manager.GET(
baseURL + (webServiceType as String) + secureParam,
parameters:[:],
success:
{ (operation: AFHTTPRequestOperation!,
responseObject: AnyObject!) in
completion(obj: responseObject)
},
failure:
{ (operation: AFHTTPRequestOperation!,
error: NSError!) in
completion(obj: nil)
})
Check you have added /api/ before your base url of API like
http:// someurl / yourBasrUrl /api/apiName
To make a valid json response, your code should look something like this:
$response = array(
"Result" => array(
"Message" => "update succeeded"
)
)
echo json_encode($response);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With