1. Home >
  2. Apps >
  3. Groups >

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

Question asked by Vishal Sharma in #PHP on Oct 19, 2014
Vishal Sharma
Vishal Sharma 路 Oct 19, 2014
Rank B3 - LEADER
I wrote the following code to make an AJAX post request to a function in my controller,
url:"{{ URL::route('res_data') }}",
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){
The problem I face is the candidate key in 'data' tag.

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');
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
but, Now another error, that says $data is non-array element. Can anyone help me with this? Posted in: #PHP
Vishal Sharma
Vishal Sharma 路 Oct 20, 2014
Rank B3 - LEADER
is there anyone who can at least give me a suggestion?
Vishal Sharma
Vishal Sharma 路 Oct 21, 2014
Rank B3 - LEADER
figured out 馃槖
Manish Goyal
Manish Goyal 路 Oct 21, 2014
Rank A2 - PRO
figured out 馃槖
dataType option?
Kaustubh Katdare
Kaustubh Katdare 路 Oct 21, 2014
Rank A1 - PRO
figured out 馃槖
Sorry that no concrete replies were received in time? Could you please share the fix for the readers?

You must log-in or sign-up to reply to this post.

Click to Log-In or Sign-Up