Click here to Skip to main content
15,904,297 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Thank you to all of you to providing so much dedicated support

Today i am getting some issue regarding Json string, I am returning some value in json and try to retrieve in Ajax Success but i am not able to get value to the variable.

What I have tried:

C#
//Return the value from registration.aspx.cs page 
 [WebMethod]
public static string verifyAadhar(string aadharNum)
{

    HttpContext context = HttpContext.Current;

    //try
    //{

    context.Session["aadharNum"] = aadharNum;        
    string url = "http://localhost:3787/api/login/login?aadharID=908765478921";
    HttpWebRequest request = HttpWebRequest.CreateHttp(url);

    request.Method = "POST";
    /*Optional*/

    request.KeepAlive = true;
    request.AllowAutoRedirect = false;
    request.Accept = "application/json, text/javascript, */*; q=0.01";
    request.ContentType = "application/json; charset=utf-8"; //"application/x-www-form-urlencoded";
                                                             ///*Optional*/
    string userId = "abc";
    string Passwd = "123456";        
    string RequestLink = context.Request.Url.Authority;

    request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("" + userId + ":" + Passwd + "")));

    using (var streamWriter = new StreamWriter(request.GetRequestStream()))
    {
        //?aadharID = 908765478921
        string json = "{\"aadharID\":\"" + 908765478921 + "\"" +  "\"}";
        streamWriter.Write(json);
        streamWriter.Flush();
        streamWriter.Close();
    }
    //try
    //{
    HttpWebResponse response = request.GetResponse() as HttpWebResponse;

        Stream stream = response.GetResponseStream();
        StreamReader sr = new StreamReader(stream);
        var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        dynamic jsonObject = serializer.DeserializeObject(sr.ReadToEnd());
        response.Close();
        sr.Close();        
    return new JavaScriptSerializer().Serialize(new { jsonObject });        
    //}
    //catch (Exception ex)
    //{   
    //    return ex.Message.ToString();
    //}
}


 //Receiving data here in ajax success
 var verfAadhar = '{"aadharNum":"' + aadharNum + '"}';
 $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "http://localhost:28331/register/registration.aspx/verifyAadhar",
        data: verfAadhar,
        datatype: 'json',
        async: false,
        success: function (data) {

            var objData = jQuery.parseJSON(data);

            $("#fname").val(objData[4]);                
        },
        error: function ()
        { console.log('Check your credentials'); }
    }); 

Please tell me how can i get my all in success data are coming like:
{"jsonObject":{"message":"success","error":false,"data":{"na‌​me":"ramdev","gen":"‌​male","dd":"10","mm"‌​:"10","yy":"1995","f‌​athername":"patanjal‌​i "}}}
Posted
Updated 29-Mar-17 18:38pm
v2

when i try to run your json in chrome console it says invalid however it is a valid json format.
{"jsonObject":{"message":"success","error":false,"data":{"na‌​me":"ramdev","gen":"‌​male","dd":"10","mm"‌​:"10","yy":"1995","f‌​athername":"patanjal‌​i "}}}

but there are some illegal unicode characters (\u200b) present in the json
refer javascript - \u200b (Zero width space) characters in my JS code. [^]

Clean format

var json =  {"jsonObject":{"message":"success","error":false,"data":{"anme":"ramdev","gen":"male","dd":"10","mm":"10","yy":"1995","fathername":"patanjali "}}}
 var fatharName = json.jsonObject.data.fathername;
 console.log(fatharName);
 
Share this answer
 
Check in client side debugger it may retrieve from the object data.
Ex:
var objData = jQuery.parseJSON(data.d);

var verfAadhar = '{"aadharNum":"' + aadharNum + '"}';
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://localhost:28331/register/registration.aspx/verifyAadhar",
data: verfAadhar,
datatype: 'json',
async: false,
success: function (data) {

var objData = jQuery.parseJSON(data.d);

$("#fname").val(objData[4]);
},
error: function ()
{ console.log('Check your credentials'); }
});
Check this code it may help you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900