0

I have an ajax call and it retunrs some value. Now i need to check the value in javascript. How can i do it

$('#cm').blur(function () {
     var cmnumber = document.forms['myform']['cm'].value;
     $.ajax({
         type: 'get',
         url: "/validatecm/" + cmnumber,
         cache: false,
         asyn: false,
         data: cmnumber,
         success: function (data) {
             alert(data)

         },
         error: function (data) {
             alert(data)

         }
     })

 });

Now i need to access the "data" in my javascript submitformfunction. Any help wil be appreciated

pktangyue
  • 8,326
  • 9
  • 48
  • 71
user2058205
  • 997
  • 2
  • 9
  • 17
  • Just need to check: Before the edit there was a script tag but no document.ready around the jQuery. Is your document ready before this is being executed (script is at the bottom of the page)? – Popnoodles Mar 01 '13 at 10:55
  • It's `async`, not `asyn`, and you should'nt turn it off. – adeneo Mar 01 '13 at 10:55
  • Just assign the data to some global variable and use it anywhere! – MJQ Mar 01 '13 at 10:56
  • *related* [How to return the response from an AJAX call from a function?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call-from-a-function) – Felix Kling Mar 01 '13 at 10:57
  • Please don't assign the data to some global variable. Using globals frivolously like that makes code harder to maintain. If that value is only to be passed to that function, that's the only place it should exist. – Popnoodles Mar 01 '13 at 10:58

2 Answers2

2
 success: function(data) {
      console.log(data);
     useInAnotherFunction(data);

 }

 function useInAnotherFunction(data)
 {
  ...use here
 }

If you use console.log(data); you can view clearly data what you have

PSR
  • 39,804
  • 41
  • 111
  • 151
1

You are already accessing the data here

alert(data)

If you still want to access it outside your success callback then make it like this

success: function(data) {
         YourFunctionCall(data)

    }
K D
  • 5,889
  • 1
  • 23
  • 35