CrazyEngineers
  • jquery AJAX call (Laravel 4 PHP): Unable to retrieve data on my Controller

    Vishal Sharma

    Member

    Updated: Oct 8, 2024
    Views: 1.0K
    I wrote the following code to make an AJAX post request to a function in my controller,
    $.ajax({
    url:"{{ URL::route('res_data') }}",
    type:"POST",
    data:{candidate:{{ json_encode($name)}}, ans : ans_array, stamps: timestamp,set:{{ $ques[0]->setnumber }}, total:{{ count($ques)}}}}).done(function(data){
    console.log(data);}).fail(function(jqXHR, ajaxOptions, thrownError){
    console.log(jqXHR);
    console.log(ajaxOptions);
    console.log(thrownError);});
    
    The problem I face is the candidate key in 'data' tag.

    json_encode($name)
    stringify's the data, On going to the route res_data, I perform a decode to convert the data received into the object. Here's my Controller function,
    publicfunction resultData(){
    $res = array();
    $count =0;
    $res =Input::get('ans');foreach($res as $r){if($r >0)
    $count = $count+1;}
    $data = json_decode(Input::get('candidate'));//error on this
    
    $entry =newResult;
    $entry->id = $data->id;
    $entry->name = $data->name;
    $entry->gender = $data->gender;
    $entry->email = $data->email;
    $entry->fblink = $data->link;
    $entry->submissions = $count;
    $entry->setnumber =Input::get('set');
    $entry->totalquestions =Input::get('total');
    $entry->answers =Input::get('ans');
    $entry->stamps =Input::get('stamps');
    $entry->save();
    return"success";
    }
    
    But, I get an error stating "json_decode() expects parameter 1 to be string, array given" The error says that the parameter is array, Hence to obtain the data as an object I tried a solution on the web and replaced the comment marked line to
    $data = json_decode(json_encode(Input::get('candidate')), FALSE);
    
    This satisfies the error I received before but now I get another error stating "preg_replace(): Parameter mismatch, pattern is a string while replacement is an array"

    I don't understand this. I even tried leaving the data as array and tried accessing data as
    $data['name']
    
    but, Now another error, that says $data is non-array element. Can anyone help me with this?
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Vishal Sharma

    MemberOct 20, 2014

    is there anyone who can at least give me a suggestion?
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberOct 20, 2014

    figured out 😒
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberOct 20, 2014

    Vishal0203
    figured out 😒
    dataType option?
    Are you sure? This action cannot be undone.
    Cancel
  • Kaustubh Katdare

    AdministratorOct 20, 2014

    Vishal0203
    figured out 😒
    Sorry that no concrete replies were received in time? Could you please share the fix for the readers?
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register