1

I am new to jQuery. I have a jQuerypiece of code which assign PAN Name to label which return from c# dictionary after conversion in JSON. When I getting value in alert(Bold Line). It is showing undefined.

Below is my code.

                    success: function (data) {
                        var msgi = data.MSG;
                        var panno;
                        if (msgi != "A" && msgi != "B") {
                            alert(msgi); ////// Here undefined is showing
                            $("#Item3_PANNumber").focus();
                            $("#Item3_PANNumber").val('');
                        }                         

                    }
/////// This is last part of method.

                            if (words.Length > 0)
                            {
                                PANCard pc = new PANCard();

                                if (words[0] == "1" && words[2] == "E")
                                {
                                    pc.PANNumber = words[1];
                                    pc.LastName = words[3];
                                    pc.FirstName = words[4];
                                    pc.MiddleName = words[5];
                                    pc.Title = words[6];
                                    pc.LastUpdated = words[7]; ////DateTime.ParseExact(words[7], @"d/M/yyyy", culture);

                                    var panno = pc.Title + " " + pc.FirstName + " " + pc.MiddleName + " " + pc.LastName;
                                    Returndict.Add("MSG", "B");
                                    return Json(panno, JsonRequestBehavior.AllowGet);
                                }
                            }

Your help may appreciated.

Thanks Nandkumar S.

4
  • Could you please make a reduced code example? (See: sscce.org) I'm fairly sure most of the code you posted is not really relevant to the problem. Also consider stepping into both the C# code and the JS code in a debugger to see what value you're returning to the webpage, and what's happening at the line of code where you get the error. Commented Jun 14, 2017 at 9:15
  • Dear millimoose, Thianks for your reply. I have shorten the code. Commented Jun 14, 2017 at 9:21
  • You're never actually returning ReturnDict from your C# code, or pc, only the panno string. Unless it's put into the JSON response outside the code shown. Commented Jun 14, 2017 at 9:23
  • Thanks millimoose. It is been resolved. Commented Jun 14, 2017 at 9:37

1 Answer 1

1

Use a class for response:

1. Create a response class

public class JsonReaponse
{
    public bool Success { get; set; }
    public string Message { get; set; } 
    public object Data { get; set; } = new List<string>();
}

2. Use a response class with object

JsonReaponse reaponse = new JsonReaponse();
if (words.Length > 0)
{
    PANCard pc = new PANCard();
    if (words[0] == "1" && words[2] == "E")
    {
        pc.PANNumber = words[1];
        pc.LastName = words[3];
        pc.FirstName = words[4];
        pc.MiddleName = words[5];
        pc.Title = words[6];
        pc.LastUpdated = words[7]; ////DateTime.ParseExact(words[7], @"d/M/yyyy", culture);

        var panno = pc.Title + " " + pc.FirstName + " " + pc.MiddleName + " " + pc.LastName;
        reaponse.Message = "Your Message";
        reaponse.Data = panno;
        return Json(reaponse, JsonRequestBehavior.AllowGet);
    }
}

            
3. In jQuery

success: function (res) {
    var msgi = res.Message;
    var panno = res.Data;
    if (msgi != "A" && msgi != "B") {
        alert(msgi); ////// Here undefined is showing
        $("#Item3_PANNumber").focus();
        $("#Item3_PANNumber").val('');
    }                         
}

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

1 Comment

Thanks Govind, This helped me a lot... :)

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.