Why Should Use JSON

Why Should Use JSON?

  1. JSON is transportation-independent, you can just bypass the XMLHttpRequest object for getting your data.
  2. You can get JSON data from anywhere, not just your own domain. There's no more proxy server nonsense.
  3. JSON parsing is generally faster than XML parsing.

How to call web services through JSON

Step 1

Make web services :

[WebMethod(EnableSession = true)]

public List<string> PageLoadScript(string ProcedureName, string parameter1)

{

string Details = "";

string Pwd = "";

List<string> returndata = new List<string>();

SqlConnection SQLCon = new SqlConnection(ConfigurationManager.ConnectionStrings["PropertyAdvice"].ConnectionString);

try

{

SqlCommand SQLCmd = new SqlCommand();

SQLCmd.Connection = SQLCon;

SQLCmd.CommandText = ProcedureName;

SQLCmd.CommandType = CommandType.StoredProcedure;

SQLCmd.Parameters.AddWithValue("@UserId", parameter1);

SQLCon.Open();

SqlDataReader dr = SQLCmd.ExecuteReader();

 

if (dr.HasRows == true)

{

while (dr.Read())

{

Details = dr["EmailId"].ToString();

Pwd = dr["Password"].ToString();

}

dr.Close();

} 

}

catch (Exception Ex)

{

return Ex.Message.ToString)();

}

finally

{

if (SQLCon.State.Equals(ConnectionState.Open))

{

SQLCon.Close();

}

}

returndata.Add(Details.ToString());

returndata.Add(Pwd.ToString());

 

return returndata;

}

 
Step 2

Then call web services using json in following way :

<script type="text/javascript>

$("document").ready(function() {

$.ajax({

type: "POST",

url: "ACWS.asmx/PageLoadScript",

data: "{'ProcedureName':'p_GetEmailPassword','parameter1':'" + $('#ctl00_head_TabContainerforall_tabAddNew_AutUserId').val() + "'}",

contentType: "application/json; charset=utf-8",

datatype: "json",

success: function(returndata) {

var data = returndata.d;

alert(data);

//alert(message.EmailId + "-" + message.Password);

},

Error: function() { alert("error"); }

});

});

</script>