Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDebugDescription = "JSON text did not start with array or object and option to allow fragments not set.";

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"}]}
like image 792
Malloc Avatar asked Mar 17 '13 00:03

Malloc


4 Answers

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.

like image 183
ajacian81 Avatar answered Oct 20 '22 11:10

ajacian81


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)
    })
like image 38
Vishal Deshai Avatar answered Oct 20 '22 09:10

Vishal Deshai


Check you have added /api/ before your base url of API like

http:// someurl / yourBasrUrl /api/apiName

like image 1
Gurjinder Singh Avatar answered Oct 20 '22 10:10

Gurjinder Singh


To make a valid json response, your code should look something like this:

$response = array(
    "Result" => array(
        "Message" => "update succeeded"
    )
)

echo json_encode($response);
like image 1
Mogens TrasherDK Avatar answered Oct 20 '22 10:10

Mogens TrasherDK