1

I want to return multiple values from Ajax call. So I modified my codes based on this page Jquery return multiple values in ajax call

$.ajax({
     type: "POST",
     contentType: "application/json; charset=utf-8",
     url: "AJAX_custom_function.aspx/AJAX_GetFullName",
     data: '{userid: "' + arguments.Value + '"}',
     dataType: "json",
     async: false,
     success: function (data) {
         alert(data);
         alert(data.fullname);    
     },
     error: function (httpRequest, textStatus, errorThrown) {
         alert("status=" + textStatus + ",error=" + errorThrown);
     }    
 });

'alert(data)' returns {"fullname": "Joe", "success" : "true"}

But 'alert(data.fullname)' returns undefined. The correct value should be Joe

Did I missing something? Any advice is very much appreciated.

AJAX_GetFullName

<System.Web.Services.WebMethod()> _
Public Shared Function AJAX_GetFullName(ByVal userid As String) As Object

    Dim isValid As Boolean = False  'by default, user always not exist
    Dim strFullName As String = ""

    isValid = IsUserIDExist(userid, strFullName)
    If isValid Then
        Return "{'fullname': '" & strFullName & "', 'success': 'true' }"
    Else
        Return "{'fullname': '', 'success': 'false' }"
    End If

End Function
3
  • please show me your WebMethod AJAX_GetFullName there is something wrong with the return type. because alert(data) should alert object Object if the WebMethod is correct. Commented Jul 22, 2011 at 8:50
  • @ChristopheCVB Data type is string Commented Jul 22, 2011 at 9:16
  • @naveen You are right, the return type is string which is wrong Commented Jul 22, 2011 at 9:16

3 Answers 3

2

Try this.

$.ajax({
    type: "POST",
    contentType: "application/json;",
    url: "AJAX_custom_function.aspx/AJAX_GetFullName",
    data: '{"userid": "' + arguments.Value + '"}',
    async: false,
    success: function (data) {
        try {
            // convert single quote to double quotes
            var msg = data.replace(/'/g, "\"");
            msg = $.parseJSON(msg);
            alert(msg.fullname);
        } catch (e) {
            alert(e.Message);
        }
    },
    error:function (xhr, status, err){
        alert( "status=" + xhr.responseText + ", error=" + err );
    }

});

No need to specify dataType and charset in contentType.

Sign up to request clarification or add additional context in comments.

5 Comments

In between, it return false for data.hasOwnProperty("d")
Thanks for your help. Not sure why, I cannot call any of these functions $.parseJSON jQuery.parseJSON JSON.parse The alert will stop working after I call any above function. So weird. I am using jquery 1.4.2 . Does it matter ?
please paste your AJAX_GetFullName method
Edited. Please refer to above.
You are the man, problem solved. My bad, I don't know is the single quote cause the issue.
1

Try using :

success: function(data) {
    if (typeof data == 'string')
    {
        data = jQuery.parseJSON(data);
    }
    alert(data.fullname);
}

4 Comments

After I run this line: data = JSON.parse(data), the alert stop working already
@Alfred: JSON is not natively supported in all browsers. you might wanna add a reference to ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js and since you are using jQuery its a lot better to use $.parseJSON
Thanks for your help. Not sure why, I cannot call any of these functions $.parseJSON jQuery.parseJSON JSON.parse The alert will stop working after I call any above function. So weird. I am using jquery 1.4.2 . Does it matter ?
Strange : version added: 1.4.1 - jQuery.parseJSON( json )
0

To convert the string to Json object use JSON.parse(data) function with in the success function of Ajax call. i hope this will help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.