I am trying to perform a nested AJAX call using the following code. The nested call doesn't seem to work. Am I doing anything wrong?
$.ajax({
type: 'GET',
url: "/public/customcontroller/dosomething",
cache: false,
dataType: "html",
success: function(html_input)
{
$.ajax({
type: 'GET',
url: "/public/customcontroller/getjobstatus",
cache: false,
dataType: "html",
success: function(html_input){
alert(html_input);
}
});
}
});
You could be having thread-type issues, given that the outside call is asynchronous and may go out of scope before the success handler can come into play. Try breaking the success call out into a separate function.
Try this
$.ajax({
type: 'GET',
url: "/public/customcontroller/dosomething",
cache: false,
dataType: "html",
async: false,
success: function(html_input)
{
$.ajax({
type: 'GET',
url: "/public/customcontroller/getjobstatus",
cache: false,
dataType: "html",
success: function(html_input){
alert(html_input);
}
});
}
});
Use the async/await syntax.
async function foo () {
var html_input = await $.ajax({
type: 'GET',
url: "/public/customcontroller/dosomething",
cache: false,
dataType: "html"
})
var html_input2 = await $.ajax({
type: 'GET',
url: "/public/customcontroller/getjobstatus",
cache: false,
dataType: "html"
})
alert(html_input2)
}
foo().catch(e => console.log('some error:', e))
Related
I have an object like this which I retrieve via an AJAX request:
{
"id": "1",
"name": "Eyes"
}
How can I get that data for my text field #id_attributes and #name_attributes? I tried this when a button was clicked, but it gives me undefined/blank?
$.ajax({
url: "to my json",
cache: false,
type: "POST",
data: 'id=' + id,
success: function (result) {
$("#id_attributes").val(result.id);
$("#name_attributes").val(result.name);
}
});
Can anyone help me?
Try:
$.ajax({
url: "to my json",
cache: false,
type: "POST",
dataType : 'json'
data:{id:id},
success: function (result) {
$("#id_attributes").val(result.id);
$("#name_attributes").val(result.name);
}
});
or parse the result
success: function (result) {
var jsonresult = JSON.parse(result);
$("#id_attributes").val(jsonresult.id);
$("#name_attributes").val(jsonresult.name);
}
Add dataType: 'json':
type: "POST",
data: {"id": id},
dataType: 'json',
success: function (result) {
add a dataType option
$.ajax({
url: "to my json",
cache: false,
type: "POST",
dataType : 'json',
data:'id='+id,
success: function (result) {
$("#id_attributes").val(result.id);
$("#name_attributes").val(result.name);
}
});
I want to retrieve parameter passes from ajax in Controller in Codeigniter but did not know how to use it my ajax function is below
function sendVideoData(frm_id)
{
var data = new FormData(document.getElementById("post_video_"+frm_id));
// make the AJAX request
jQuery.ajax({
type: "POST",
url: "<?php echo base_url(); ?>"+"dashboard/do_upload",
data: data+'&form_id='+frm_id,
mimeType:"multipart/form-data",
contentType: false,
cache: false,
processData:false,
dataType: 'json',
success: function (data) {
alert(data);
},
});
return false;
}
</script>
I want to retrieve
form_id
in my controller and use it
It's pretty simple,
echo $this->input->post('form_id')
or simple php
echo $_POST['form_id']
inside your controller dashboard and method do_upload
You can get all list of post variables using print_r($this->input->post()) as well.
Your AJAX request can be:
jQuery.ajax({
type: "POST",
url: "<?php echo base_url() ?>dashboard/do_upload",
data: 'form_id=' + frm_id + '&data=' + data,
mimeType: "multipart/form-data",
contentType: false,
cache: false,
processData:false,
dataType: 'json',
success: function (data) {
alert(data);
},
});
And in ur controller: $this->input->post('form_id');
I have code in my html file as below. I am using jQuery Mobile
$.ajax({
type: "GET",
url: "http://localhost/owner_pickup.php",
cache: false,
dataType:'json'
success: function(data)
{
// On success
}
});
owner_pickup.php returns me data by executing query. Now i need to pass a value which i would read in my owner_pickup.php file.
Kindly suggest me how would we pass the value
in your php file:
$value = array(
"dat_1" => "this is data number 1",
"dat_2" => "this is data number 2"
);
echo json_encode($value);
in your jquery finction:
$.ajax({
type: "GET",
url: "http://localhost/owner_pickup.php",
cache: false,
dataType:'json'
success: function(data)
{
var value1 = data.dat_1;
var value2 = data.dat_2;
}
});
please look at this answers:
retrieve multiple values from ajax call
if you don't know how to use JSON please google it.
edit:
pass a value to the php:
$.ajax({
type: "GET",
url: "http://localhost/owner_pickup.php",
cache: false,
data: {
first_value:50,
second_value:55
}
dataType:'json'
success: function(data)
{
var value1 = data.dat_1;
var value2 = data.dat_2;
}
});
in the php:
if(isset($_GET['first_value']))
$first = $_GET['first_value'];
$.ajax({
type: "GET",
url: "http://localhost/owner_pickup.php",
data: {param1: 123, param2: "text value"},
cache: false,
dataType:'json',
success: function(data) { // On success }
});
$.ajax({
type: "GET",
url: "http://localhost/owner_pickup.php",
data:{key1:value1}
cache: false,
dataType:'json'
success: function(data)
{
}
});
In php aaccept it as
<?php
$_REQUEST['key1'];
?>
I am noob at using jquery and ajax. I need to change the form from $.post to $.ajax .
var disqus_config = function() {
this.callbacks.onNewComment = [function(comment) {
$.post("sendnotification", { comment: comment.id, post: $post->id,author:$author->id}, function(result){
alert(result);
});
}];
};
I know I need to end something like here but I am stuck how to use post datas(comment,post,author) inside this function
$.ajax({
url: 'sendnotification',
type: 'POST',
data: 'query=' + query ,
dataType: 'JSON',
async: true,
success: function(data){
process(data)
}
Thanks
Just use the same object literal you did for $.post, eg (gotta assume that's some PHP or something in there)
$.ajax({
url: 'sendnotification',
type: 'POST',
data: { comment: comment.id, post: {$post->id}, author: {$author->id} },
dataType: 'json',
async: true,
success: function(data){
process(data)
}
});
I believe dataType: 'JSON' should be changed to dataType: 'json'
Also, use the same data array as you used in your $.post variant.
$.ajax({
url: 'sendnotification',
type: 'POST',
data: { comment: comment.id, post: $post->id,author:$author->id } ,
dataType: 'json',
async: true,
success: function(data){
process(data)
}
});
I use codeigniter and i want make uniqid code with php as following code(by random_string()):
function coding(){
echo random_string('unique');
}
I want use result top function in a, jquery function (for use in js code), i in jquery code try as this, but this output in alert(coding()) is "undefined". How can fix it?
function coding(){
$.ajax({
type: "POST",
dataType: "text",
url: "coding",
cache: false,
success: function (data) {
return data;
}
})
}
alert(coding()) // in here output is "undefined" !!!?
function coding(){
var temp;
$.ajax({
type: "POST",
dataType: "text",
url: coding,
cache: false,
async: false,
success: function (data) {
temp = data;
}
})
return temp;
}
alert(coding());
You will receive an [object XMLDocument].
It is becuse coding function is asynchronous.
Use follow code:
function coding(){
$.ajax({
type: "POST",
dataType: "text",
url: coding,
cache: false,
success: function (data) {
alert(data);
}
})
}
coding();
function coding(){
$.ajax({
type: "POST",
dataType: "text",
url: "coding",
cache: false,
success: function (data) {
$("#yourDiv").html(data);
}
})
}
You can assign value to another element like div, p etc.
Well, I'm assuming that the coding variable you are passing at the parameter url is a reference to a method in your CI controller.
If that's the case, you just need to wait from the success callback:
$.ajax({
type: "POST",
dataType: "text",
url: coding,
cache: false,
success: function (data) {
alert(data);
}
})
Ok, you should use callback function like this:
function coding(func){
$.ajax({
type: "POST",
dataType: "text",
url: coding,
cache: false,
success: function (data) {
func(data);
}
})
}
coding(function(data)
{
alert(data);
});