This question already has answers here:
AJAX not passing data to CodeIgniter Controller
(5 answers)
Closed 4 years ago.
is there anyway that i can pass a codeigniter session value through ajax and get results back ?my ajax request as follows
$.ajax({
method :'GET',
url: baseUrl+'ajaxcontroller/LoadData_To_View',
success:function(data){
$('#item').html(data);
console.log(data);
},
complete: function(){
$('#loadingImage2').hide();
},
error:function (xhr, ajaxOptions, thrownError){alert(thrownError);}
});
Yes you can actually! You can actually pass any variable through ajax through the data property but it has to be in a JSON Format. Here's the code below.
First Example:
$.ajax({
method: 'GET',
url: baseUrl + 'ajaxcontroller/LoadData_To_View',
data: {
id: "<?= $this->session->userdata('id') ?>",
username: "<?= $this->session->userdata('email') ?>"
},
success: function(data) {
$('#item').html(data);
console.log(data);
},
complete: function() {
$('#loadingImage2').hide();
},
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError);
}
});
OR you can pass the whole session like this!
$.ajax({
method: 'GET',
url: baseUrl + 'ajaxcontroller/LoadData_To_View',
data: <?= json_encode($_SESSION) ?>,
success: function(data) {
$('#item').html(data);
console.log(data);
},
complete: function() {
$('#loadingImage2').hide();
},
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError);
}
});
What matters is you have to have your variables in a JSON format for it to work! A JSON Format will be translated as a multidimensional array in the server side.
Related
I want to pass the response data to another page in ajax success.
I'm using jquery in laravel. I have called the function and get the data from the controller in success call. I want to pass the received data to another page detail
$.ajax({
type: 'get',
url: 'am_detailed_report', //send the request to the controller
data: {
am_geo_selection: am_geo_selection
},
dataType: 'json',
success: function(data) {
console.log(data);
$url = "am_detailed?filter=" + data;
window.open($url, "_blank"); //want to send the parameter in post instead of passing it in url.
$('.loaderImage').hide();
},
error: function(jqXHR, textStatus, errorThrown) {
$('.loaderImage').hide();
}
});
Try this
$.ajax({
type: 'get',
url: 'am_detailed_report', //send the request to the controller
data: {
am_geo_selection: am_geo_selection
},
dataType: 'json',
success: function(data) {
console.log(data);
$url = "am_detailed"
const html = `<form action="${url}" method="post">
<textarea name="filter">${data}</textarea>
</form>`
const w = window.open("", "_blank");
w.document.write(html);
w.document.close();
w.document.querySelector("form").submit()
$('.loaderImage').hide();
},
error: function(jqXHR, textStatus, errorThrown) {
$('.loaderImage').hide();
}
});
Here is my code and its not print or display my json data in ckeditor
function getRules(){
$.ajax({
url: "api",
type: "POST",
data: {
version:'0.1'
},
dataType: "json",
success:function(response){
console.log(response.data.recordslist.RulesDetails);
CKEDITOR.instances.txtEdit.setData(response.data.recordslist.RulesDetails);
},
error: function (xhr, ajaxOptions, thrownError) {
//swal("Error!", "Please try again", "error");
}
});
}
If Your URL is correct and you have URL by the name of api only edit success function to
success:function(response){ $('#txtEdit').text(response); }
this show a result in array you need to extract the array.
if you don't want to extract the array. remove
dataType: "json"
it work fine. and write the data in text editor.
here is about a javascript widget DataTables. An example can be found here.
sorry, i am not a javascript specialist. How can i transfer the selected row (practically my objects from server) back to the server in form of json-format ?
i did try to do it with this approach, but it doesn't work:
$('#save_btn').click( function () {
//table.row('.selected').remove().draw( false );
console.log ( table.rows('.selected').data());
var stringData = table.rows('.selected').data().serialize();
$.ajax({
url: '${pageContext.request.contextPath}/ajax/storeSelectedContacts',
data: stringData ,
type: "POST",
cache: false,
success: function (savingStatus) {
alert("success");
},
error: function (xhr, ajaxOptions, thrownError) {
alert("error")
}
});
} );
many thanks
First, its return array of objects.
var stringData = table.rows('.selected').data();
Second, for convert array to JSON ...
var aData = table.rows('.selected').data();
var sData = JSON.stringify(aData)
and for send to server you slould indicate that is JSON dataType: 'json'
$.ajax({
url: '${pageContext.request.contextPath}/ajax/storeSelectedContacts',
data: sData ,
type: "POST",
cache: false,
dataType: 'json',
success: function (savingStatus) {
alert("success");
},
error: function (xhr, ajaxOptions, thrownError) {
alert("error")
}
});
} );
This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 8 years ago.
Hello I have a problem storing my json in a global variable, what I want is to do first all my ajax request then store each of the returned data to a global variable, but it seems it's not working correctly? Can Any help me to my problem? Thanks. :)
var series;
function columnChart(container)
{
url = base_url+"/codeigniter/index.php/AssistanceMonitoringModule/assistanceMonitoring/getSeries";
$.ajax(
{
type: "GET",
url: url,
success: function(data){
series = data;
},
dataType: "json",
error: function (xhr, ajaxOptions, thrownError) {
alert("XHR:"+xhr.status+"Error:"+thrownError);
}
});
callColumnChart(container,series);
}
You are using the variable too soon. The Ajax request will not be completed when you call callColumnChart. Move it into the ajax callback.
$.ajax(
{
type: "GET",
url: url,
success: function(data){
series = data;
callColumnChart(container,series);
},
dataType: "json",
error: function (xhr, ajaxOptions, thrownError) {
alert("XHR:"+xhr.status+"Error:"+thrownError);
}
});
How to access this json data in JavaScript.
when I alert it the result is undefined
Here is jQuery code
$.ajax({
type: "POST",
url: "frmMktHelpGridd.php",
data: {
labNo: secondElement
},
dataType: "json",
beforeSend: function () {
// Do something before sending request to server
},
error: function (jqXHR, textStatus, errorThrown) {
alert('error has occured');
alert(errorThrown);
},
success: function (data) {
//Here is the problem
alert(data[0]['Result']);
}
});
This is PHP code
$data=array($no);
for($i=0;($i<$no && ($row=mysql_fetch_array($result)));$i++)
{
$data[$i]=array();
$data[$i]['Result'] = $row['Result'];
$data[$i]['TestCode'] = $row['TestCode'];
$data[$i]['TestStatus'] = $row['TestStatus'];
$data[$i]['SrNo'] = $row['SrNo'];
}
$data1=json_encode($data);
echo $data1;
exit;
I have tested the PHP file independently,
The json data is output as follows:
[{"Result":"1","TestCode":"22","TestStatus":"0","SrNo":"1"},{"Result":"1","TestCode":"23","TestStatus":"1","SrNo":"2"}]
$.ajax({
type: "POST",
url: "frmMktHelpGridd.php",
data: {
labNo: secondElement
},
dataType: "json",
beforeSend: function () {
// Do something before sending request to server
},
error: function (jqXHR, textStatus, errorThrown) {
alert('error has occured');
alert(errorThrown);
},
success: function (data) {
//Added parse json
var data = jQuery.parseJSON(data)
alert(data[0]['Result']);
}
});
You can access to your data by doing
data[0].Result
It's an Object, not an array.
so data[0]['Result'] it's not the proper way
EDIT:
Since you have more objects, you have to do a loop this way:
$.each(data, function(key, val){
console.log(val.Result);
console.log(val.TestCode);
//...
});
When you see something like
{
"foo":"bar",
...
}
you can access to it the same way as above:
name_of_the_object.foo
that will have the value "bar"
Try to add parse JSON. I have added. Now it may be work.
$.ajax({
type: "POST",
url: "frmMktHelpGridd.php",
data: {
labNo: secondElement
},
dataType: "json",
beforeSend: function () {
// Do something before sending request to server
},
error: function (jqXHR, textStatus, errorThrown) {
alert('error has occured');
alert(errorThrown);
},
success: function (data) {
//Added parse json
var data = $.parseJSON(data)
alert(data[0]['Result']);
}
});