Shakeel
Shakeel

Reputation: 21

Ajax call in Javascript

I want to get value in JavaScript using Ajax Call,
I am using the following code:

var value = $.ajax({
    type:"GET",
    url:"get_result.php",
    data:"{'abc':" + $abc + "}",
});

alert(value);

while I wrote following code in get_reult.php:

<?php
    echo $abc= "Working";
?>

Happy to know about good solution

Upvotes: -1

Views: 87

Answers (3)

Scary Wombat
Scary Wombat

Reputation: 44814

The ajax call is asynchronous so that the result is probably not available when you call alert(value);

You will need to put this code into a success block.

$.ajax({
    type:"GET",
    url:"get_result.php",
    data:"{'abc':" + $abc + "}"
}).success (function(value) 
{
  alert(value);
});

Upvotes: 0

Nanhe Kumar
Nanhe Kumar

Reputation: 16297

$.ajax({
  url: 'get_result.php',
  type: 'GET',
  data: 'abc='+$abc,
  success: function(data) {
    //called when successful
    alert(data);
  },
  error: function(e) {
    //called when there is an error
    //console.log(e.message);
  }
});

Upvotes: 1

Elad Stern
Elad Stern

Reputation: 1071

You're looking for the success parameter in your call:

<script>
$.ajax({
    type:"GET",
    url:"get_result.php",
    data:"{'abc':" + $abc + "}",
    success: function(result) {
        alert(result);
    }
});
</script>

Read more about AJAX calls with jQueryhere

Upvotes: 0

Related Questions