Problem sending an array containing some JSON string to AJAX from PHP - php

Am working on a form whereby am passing values from the frontend to the backend using AJAX. From the frontend, all the data is being passed fine except that after performing some logic on the backend, I need to transport the data to the frontend. The data is contained in 2 separate variables whereby I have converted each to a JSON object for transmission.
When I dd() the data in the backend I get it in form of a string.
The problem is when I log response in the console tab (from the AJAX code), I don't get any response from the backend.. Please assist?
Controller file containing PHP code
public
function validatePlanEntries(Request $request)
{
//dd($request->all());
//Other PHP logic
//Convert data to JSON format
$form = json_encode($oldata);
//dd($form);
$planJson = json_encode($plans_benefits);
$plans = compact(['planJson' , 'form']);
//dd($plans);
return $plans;
}
AJAX code getting the response from the controller above
<script>
//Other Js code
form.parsley().validate();
//Returns true if Parsley validation has no errors
if (form.parsley().isValid()){
$.ajax({
type: "POST",
url: "getplans",
data:JSON.stringify(type),
contentType: 'application/json',
dataType: "json",
success: function(response){
console.log(response);
},
failure: function(errMsg) {
alert(errMsg);
}
});
};
</script>

I wouldn't convert them to 2 separate JSON encoded values an instead add them to 1. So instead of...
//Convert data to JSON format
$form = json_encode($oldata);
//dd($form);
$planJson = json_encode($plans_benefits);
$plans = compact(['planJson' , 'form']);
//dd($plans);
return $plans;
Something like, build an array with both pieces of data and json_encode() the result...
return json_encode([ "form" => $oldata, "plan" => $plans_benefits]);
You will need to update your Javascript to extract the relevant parts of the response.
Update:
I'm not a Laravel person, but it seems you can use...
return \Response::json([ "form" => $oldata, "plan" => $plans_benefits]);

Related

Passing via GET json to ajax jquery,

I'm getting from a php page a json after a json encode. I correctly see the json in page that i want to pass to ajax. The data i send are taken via get from a form in php, saved in an array and then passsed with json encode.
But i get the error object from ajax:
What i'm missing? Should i copy and paste part of my backend too?
$.ajax({
method: "GET",
url: "queries/queries.php",
dataType: "json",
succes: function(data){
console.log(data);
console.log('json found');
},
error: function(error){
console.log(error);
console.log('json not found');
}
});
backend:
$calls= "text";
for ($i=0;$i<count($array);$i++) {
$schemaTab = $array[$i] . $calls;
$query = "SELECT * from 'schema'.'table'"; // this is just a sample query
$res=$db->getQuery($query); // this is a function that output the db query
header('Content-Type: application/json');
echo json_encode($res); // the json i pass
}
json:
[{"Schema":"schemaName1","DATEHOUR":"10\/05\/2021 11:56","count":"4"}]
[{"Schema":"schemaName2","DATEHOUR":"10\/05\/2021 10:21","count":"3"}]
[{"Schema":"schemaName3","DATEHOUR":null,"count":"0"}]
[{"Schema":"schemaName4","DATEHOUR":null,"count":"0"}]
Your JSON is invalid. JSON must consist of one thing such an a string, object, or array. (Objects and arrays can nest as many other things as they like inside them).
You have an array ([{"Schema":"schemaName1","DATEHOUR":"10\/05\/2021 11:56","count":"4"}]) which would be valid JSON but then you have another array and so on.
Gather up all your data into a variable in your PHP.
Then have header('Content-Type: application/json'); echo json_encode($that_variable) once, outside the loop.

Use the autocomplete event in the succes of my ajax

I would like to use an autocompletion in my application. I'm trying to use the jquery UI completion but nothing happens. I made an ajax to get all columns with a specific variable written by the user. The query is working, I have my array with all my columns back from the server. With this query reponse, I tried to do the jquery autocompletion in the success ajax but as I said nothing is happening.
Do you have an idea?
function autoCompleteRegate(){
$("#code_regate").keyup(function() {
// AJAX de l'auto-complete
var source = '/gestion/gestDepot/ajaxautocompleteregate';
var codeRegate = $("#code_regate").val();
$.ajax({
type : "POST",
url : source,
async : false,
dataType : 'json',
data : {
'codeRegate' : codeRegate
},
success : function(response) {
var availableTags = response;
$("#code_regate").autocomplete({
source: availableTags
});
}
});
});
public function ajaxautocompleteregateAction()
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$params = $this->_getAllParams();
$codeRegate = $params['codeRegate'];
$oDepotService = new Services_Depot();
$response = $oDepotService->searchCodeRegate($codeRegate);
echo json_encode($response);
}
Network query - form
Exemple of nothing happening
The answer from the server
You have to directly pass the cd_regate array instead of a multidimensional array. One workaround is you could process the json output on the backend side :
public function ajaxautocompleteregateAction()
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$params = $this->_getAllParams();
$codeRegate = $params['codeRegate'];
$oDepotService = new Services_Depot();
$response = $oDepotService->searchCodeRegate($codeRegate);
$json = [];
foreach ($response as $key => $value) {
array_push($json, $value->cd_regate); // the output will be "[774970, 774690, 774700,... ]"
}
echo json_encode($json);
}
I would suggest the following for your JavaScript:
$("#code_regate").autocomplete({
source: function(request, response){
$.ajax({
type : "POST",
url : '/gestion/gestDepot/ajaxautocompleteregate',
async : false,
dataType : 'json',
data : {
'codeRegate' : request.term
},
success : function(data) {
response(data);
}
});
}
});
This uses a function as a Source. From the API:
Function: The third variation, a callback, provides the most flexibility and can be used to connect any data source to Autocomplete, including JSONP. The callback gets two arguments:
A request object, with a single term property, which refers to the value currently in the text input. For example, if the user enters "new yo" in a city field, the Autocomplete term will equal "new yo".
A response callback, which expects a single argument: the data to suggest to the user. This data should be filtered based on the provided term, and can be in any of the formats described above for simple local data. It's important when providing a custom source callback to handle errors during the request. You must always call the response callback even if you encounter an error. This ensures that the widget always has the correct state.
When filtering data locally, you can make use of the built-in $.ui.autocomplete.escapeRegex function. It'll take a single string argument and escape all regex characters, making the result safe to pass to new RegExp().
When 1 or more letters are entered into the text field, this will be passed to the function under request.term and you can POST that to your script via AJAX. When you get the result data, it must be in an Array or an Object with the right format.

Error in accessing post json data in yii2

Am passing data to yii2 using ajax request but i keep on getting a 500 error
This is the ajax request code:
<?php
$script = <<< JS
$('form#forward_pr').on('beforeSubmit', function(e){
var keys = $('#grid').yiiGridView('getSelectedRows');
$.post({
url: "forwardpr", // your controller action
dataType: 'json',
data: {keylist: keys},
success: function(data) {
alert('I did it! Processed checked rows.')
},
error: function(err){
console.log("server error");
}
});
return false;
} ) ;
JS;
$this->registerJS($script);
?>
When i do console.log(keys) this returns
[0, 1]
This is my controller code:
if (Yii::$app->request->post()) {
echo $post = json_encode($_POST['keys']);
if (isset($_POST['keylist'])) {
$keys = \yii\helpers\Json::decode($_POST['keylist']);
print_r($keys);
}else{
echo "1";
}
The above always executes the error part of post request, What could be wrong;
You are sending your JSON as encoded (post) data body, not key value pairs. So your approach is not working this way.
There are two options to fix this:
refactor your controller into a RESTful service
in your controller use the JSON body rather than POST parameters
While the first option is preferred in the long run, the second option is pretty simple as a quick fix.
First, make sure you configure your app to parse JSON body conten.
IN config.php add this to the components array:
'request' => [
'parsers' => [
'application/json' => 'yii\web\JsonParser',
]
]
Then in your controller use this to get the JSON parameters:
$model->load(Yii::$app->getRequest()->getBodyParams());
I'm a newbie.. But I also want use checkboxcoloumns in gridview (Kartik version).
1st thing.
Instead of writing
var keys = $('#grid').yiiGridView('getSelectedRows');
I have to write
var keys = $('#w4').yiiGridView('getSelectedRows');
2nd thing.
In the controller you can process the keylist, but don't try to decode it, simple use it int this way:
$keys = $_POST['keylist'];
and it seems it works for me!
Sorry for my english..

AJAX request to PHP - Handling response data

Im working on some AJAX login function, making a request via PHP.
I am wondering how do I return data via the PHP so that I can handle it like the code mentioned below. I only worked on the javascript side and not the PHP server side so I am confused now.
I want to get a value/values stored in the response like data.name , data.sessionID , data.listOfnames
Is the data a JSON Object by itself?
Thanks!
The code below was used before and has been verified to work.
function retrieveVersion(){
var URL = RSlink + "/updates";
$.ajax({
headers : {
"Content-Type" : "application/json; charset=UTF-8",
"sessionid" : result
},
type : "GET",
url : vURL,
statusCode : {
0 : function() {
hideLoadingMsg();
showError(networkError, false);
},
200 : function(data) {
currentVersion = String(data.version);
updatesArray=data.updates;
});
}
}
});​
Suppose you have a php file for handling ajax calls. It should be available by this url - vURL.
In this file you need to create an array.
Something like this:
$data = array('data' => array('version' => yourversion, 'updates' => yourupdates));
Then you need to convert this object into json string. You can do this using json_encode() function
echo json_encode($data);
One thing to keep in mind: You need to echo your data and only. If somewhere in your file you will have some other echo-es, the result string returning to your ajax-funtion might be broken.

Sending json to symfony controller

I need to pass json data to my Symfony Controller. My ajax function looks like this:
var data = '{"firstname":"John"}';
$.ajax({
type: "POST",
url: save_url, //path to controller action
data: {json:data},
success: function(response) {
// Do something
}
});
In my controller, I try to get my data through:
public function createAction(Request $request) {
$data = $this->getRequest()->get('firstname');
return $this->render('MyBundle:Counter:test.html.twig', array(
'data' => $data
));
Just to see if this works, I send $data to be echoed in a template. In Firebug I can see the data being sent and everything seems to work, but $data is empty and nothing is echoed. Where am I doing this wrong?
EDIT: When I check the response in Fireburg console, I see my data there, in place, but it never appears in the template. var_dump($data) tells that $data is null. So, it seems data is being sent but the controller ignores it.
As Marek noticed:
$this->getRequest()
already returns the request object, you're accessing the request property of the request, that doesn't add up. Either try:
$data = $this->request->get('json');
Or use:
$data = $this->getRequest()->get('json');
You can, of course assign the return value of $this->getRequest() to a variable, and call the get method on that var from there on end... anyway, here's my initial answer, it does contain some more tips, and considerations you may find useful:
You should be able to get the data this way, though AJAX requests + echoing in a template? That does sound a bit strange. I don't see you passing the $data variable to a $this->render call anywhere.
This is a copy-paste bit from a controller action in one of my projects. It works just fine there:
public function indexAction()
{
if (!$this->getRequest()->isXmlHttpRequest())
{//check if request is AJAX request, if not redirect
return $this->redirect(
$this->generateUrl('foo_bar_homepage')//changed this, of course
);
}
$id = $this->getRequest()->get('id',false);//works fine
However, I can't begin to grasp why you're doing this:
var data = '{"firstname":"John"}';
Why not simply go for:
$.ajax({
type: "POST",
url: url,//post how you get this URL please...
data: {firstname: 'John'},//jQ will sort this out for you
success: function(response)
{
console.log(response);
}
error: function()
{
console.log('an error occured');
console.log(arguments);//get debugging!
}
});
Then, in your controller you're able to:
$this->getRequest()->get('firstname');//it should be John
You could even pass {json:{firstname: 'john'}} as the data param to $.ajax, the only difference in your controller will be, that you have to do this:
$data = $this->getRequest()->get('json');
$firstName = $data['firstname'];
That should work just fine, unless there's somthing you're not telling us :)
RECAP:
This is what I'd write:
public function createAction()
{//no Request param in controller
if (!$this->getRequest()->isXmlHttpRequest())
{//no ajax request, no play...
$this->redirect(
$this->generateUrl('homepage_route')
);
}
$data = $this->getRequest()->get('firstname');
//return json response:
return new Response(json_encode(array('dataReceived' => $data));
//return rendered HTML page:
return $this->render('MyBundle:Counter:test.html.twig', array(
'data' => $data
));
}
Of course, then the JS code should read:
$.ajax({
type: "POST",
url: 'route/to/create'
data: {firstname:'John'},
success: function(response)
{
console.log(response);
}
});
I have tested this, and I see no reason why this shouldn't work. It works just fine for me...
Please note this was #EliasVanOotegem original example but there are some obvious steps missing
in the controller i'm reading a few replies as in "I cannot see how this works as i'm getting null" this is because your not correctly keying your object.
i.e.
var data = { name : 'john' };
$.ajax({
type: "POST",
url: url,//post how you get this URL please...
data: {json : data},//jQ will sort this out for you
success: function(response)
{
console.log(response);
}
error: function()
{
console.log('an error occured');
console.log(arguments);//get debugging!
}
});
as you can now see accessing the requerst object like
$request->get('json');
refers to the post key for the json data
Is the content what you're trying to retrieve, neither params nor headers.
Try:
$request->getContent();
In your case $request->request->get('json') should do.

Categories