JavaH
JavaH

Reputation: 427

How to return the value from ajax

I have used the jquery ajax for getting the values from the server.when the method goes into the ajax success or error, i need to return the value.How to do that.Please guide me.

$.ajax({
    cache: false,
    async: true,
    type: "GET",
    timeout:6000,
    dataType: "json",
    url:url +"log",
    data: { ContactEmail : $("#username").val()},
    contentType: "application/json;charset=utf-8",
    success: function (result) 
    {
        //Here I need to return the result and should get in another method
    },
    Error: function (e)
    {
        //Here I need to return the result and should get in another method
    }
});

update

for example when i called the checkNetConnection() ,it return the value.i need like this

function checkNetConnection() 
{
        var netresult=0;
        var networkState = navigator.network.connection.type;
        var states = {};
        states[Connection.UNKNOWN]  = 'Unknown connection';
        states[Connection.ETHERNET] = 'Ethernet connection';
        states[Connection.WIFI]     = 'WiFi connection';
        states[Connection.CELL_2G]  = 'Cell 2G connection';
        states[Connection.CELL_3G]  = 'Cell 3G connection';
        states[Connection.CELL_4G]  = 'Cell 4G connection';
        states[Connection.NONE]     = 'No network connection';
        if(states[networkState]=='Ethernet connection'||states[networkState]=='WiFi connection' || states[networkState]=='Cell 2G connection' || states[networkState]=='Cell 3G connection' || states[networkState]=='Cell 4G connection')
        {
            netresult=1;
        }
        else
        {
            netresult=0;
        }
        return netresult;
}

Upvotes: 0

Views: 159

Answers (4)

Nikola K.
Nikola K.

Reputation: 7155

Your results are stored in variables result and e:

...
success: function (result) 
    {
        // result contains the results
        foo( results );
    },
    Error: function (e)
    {
        // e contains the error
        bar( e );
    }
...

// Function to do something with the returned results
function foo( someData ) {
  // do stuff
}

// Function to do something with the error
function bar( someErrorData ) {
  // do stuff
}

A quick way of seeing the data is to display it using alert: alert( results ) or alert( e ) instead of the function calls.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038830

There are a couple of issues with your code. You have specified contentType: "application/json;charset=utf-8" but you are not sending JSON request. If you want to send a JSON request use the JSON.stringify method on your data:

data: JSON.stringify({ ContactEmail : $("#username").val() }),

If not, remove this contentType, otherwise it is ambiguous for the server. Another issue with your code is that the error callback is called error, not Error. And as far as fetching the results is concerned, inside the success callback they will be passed directly as argument:

success: function (result) {
    // Here I need to return the result and should get in another method
    anotherMethod(result);
},

It should also be noted that the argument passed to the success method will automatically be parsed by jQuery to the underlying type if your web server set the correct content type response header. So for example if your server set Content-Type: application/json and wrote {"foo":"bar"} to the response body, you can directly manipulate this object: alert(result.foo); inside this callback without additional parsing. If your web server script is poorly written and doesn't set the proper content type header, for example it sets Content-Type: 'text/html' and write a JSON formatted string into the response body, you have the possibility to force jQuery to parse to JSON using the dataType: 'json' parameter. But normally, if everything is correct you shouldn't need to do that.

Now, the error callback is different. It is passed the xhr object as argument, so you will have to extract the result as string this time:

error: function(xhr) {
    anotherMethod(xhr.getResponseText());    
}

If this string represents JSON you could parse it:

error: function(xhr) {
    var result = $.parseJSON(xhr.getResponseText());
    anotherMethod(result);    
}

Upvotes: 5

Suave Nti
Suave Nti

Reputation: 3747

  success: function (result) 
            {

              alert(result.someobject); //someobject is returned by your JSON
            },

Upvotes: 0

Joyce Babu
Joyce Babu

Reputation: 20654

In the success method result is a json object which contains the data you sent from your server.

Use console.log(result) to see the data.

            success: function (result) 
            {
              console.log(result);
            },
            Error: function (e)
            {
            //Here I need to return the result and should get in another method
            }

Upvotes: 0

Related Questions