0

I'm trying to upload a file, and some data to my server, using Angular JS to make a POST request via AJAX, while my back end is Laravel 4 (not that it matters much here).

Now I successfully pass this data to my controller. I'm able to do the following:

$data = Input::all();
return $data;

I console.log the data out when the response is successful, and it produces:

Object {
    imageData: "{"title":"Some Title","owner":"Owner Name","source":"Source Name"}", 
    file: Object
}

Okay so this is what I want, but more specifcically I want the stuff inside of imageData.

    $data = Input::all();

    $imageData = $data['imageData'];

    return $imageData;

Okay so console.log that back:

Object {title: "Some Title", owner: "Owner Name", source: "Source Name"}

Great - but now lets grab the individual elements following that trend:

    $data = Input::all();

    $imageData = $data['imageData'];

    $title = $imageData['title'];

    return $title;

Nope: 500 (Internal Server Error). Same happens why I try to access it as an object ($title = $imageData->title.

So how am I supposed to grab the stuff inside of there?

3
  • Check your error logs to see what the 500 error really means. Commented Feb 27, 2014 at 18:57
  • could you print_r($imageData) just to see what it looks like from php Commented Feb 27, 2014 at 18:57
  • @JonathanKuhn exception 'ErrorException' with message 'Illegal string offset 'title'' - not much else! Commented Feb 27, 2014 at 19:05

1 Answer 1

4

Your problem is that $imageData is a string. That string happens to contain JSON, but it's still a string none the less. You can tell by the fact that it starts and ends with a double quote ", which wouldn't be the case if it were actually JSON.

You problem is likely that you're passing JSON to something, which then converts the data to JSON.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, yes I think you're right! And after some research a json_decode( $data['imageData'], true) seemed to do the job. Ta.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.