如何在ajax成功中获取我的Json格式数据?

时间:2017-03-29 11:11:13

标签: c# jquery asp.net json ajax

感谢大家提供如此多的专业支持

今天我得到一些关于Json字符串的问题,我在json中返回一些值并尝试在Ajax Success中检索但是我无法获得变量的值

 //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'); }
    }); 

请告诉我如何获得我的所有成功数据,例如[name,fname,dob,gen,mob。

1 个答案:

答案 0 :(得分:0)

谢谢你们所有人都回答了我的问题。

public DataModel() {
        super();
    }

}

通过使用我可以从json获取我的值的方法..