Skip to content Skip to sidebar Skip to footer

Assign $.ajax To Variable

I'm not getting any output the id_function.php works perfectly fine can anyone please help me? JavaScript function get_details() { var oriz = document.getElementById(

Solution 1:

try instead this because when you make the alert the ajax call can't be finish yet. With the suucess function you are secure that the ajax call is finished, you have to echo something into your php file if you want a response:

$.ajax({
   type: "POST", 
   url: 'id_function.php',
   dataType: "text", 
   data: { 'orig': oriz, 'des_id' : dizz },
   async: false,
   success: function(data){
      alert(data);
   }
})

Into your php file you can take values in this mode:

<?php
$orig = $_POST['orig'];
$des_id = $_POST['des_id'];
?>

Solution 2:

This will not work because AJAX calls are asynchronous which means that they don't immediately return a value after you execute them. It has to go out to the server, let the serverside code run and then provide a value back. Think of it as similar in many ways to kicking off a thread in another language; you have to wait for the thread to yield control back to your application.

var id_orig_diz = $.ajax({
    type: "POST", 
    url: 'id_function.php?orig='+oriz+'&des_id='+dizz,
    dataType: "text", 
    async: false
}).responseText;

What you need to do is set a result handler like this:

var id_orig_diz;
$.ajax({
    type: "POST", 
    url: 'id_function.php?orig='+oriz+'&des_id='+dizz,
    dataType: "text", 
    async: false,
    success: function(data){
        id_orig_diz = data; //or something similar
    }
});

Solution 3:

The recommended method is to use a success function to receive the data and assign your ID. From the documentation:

success

Type: Function( PlainObject data, String textStatus, jqXHR jqXHR )

You should use the data parameter to get your ID.


The code is the same as in Alessandro's answer:

success: function(data){
    alert(data);
}

Post a Comment for "Assign $.ajax To Variable"