Reputation: 3912
I'm getting the following error when try to read json data from asp.net function, here is the image error
Here is the jQuery code,
<script type="text/javascript">
$(document).ready(function () {
$("#getdata").click(function () {
$.ajax({
type: "POST",
url: "Default.aspx/GetData",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
alert(data.d);
// $("#company").html(data.d);
console.log(data);
},
error: function (error) {
alert(error);
}
});
});
});
</script>
what wrong I did to get this error. Appreciate your any help.
getting this error from console log,console.log(error)
Upvotes: 0
Views: 11516
Reputation: 3912
Finally, I found what was the issue, I have to add "[WebMethod()]" declaration just before the asp.net function. For example,
[WebMethod()]
public static string GetData()
{
try
{
string strjson;
string connectionstring = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
SqlConnection Con = new SqlConnection(connectionstring);
DataSet DS = new DataSet();
String CmdText = "Select Compname,compadd1,compadd2,compemail from aas_company where compid=@cmpid";
SqlCommand cmd = new SqlCommand(CmdText, Con);
cmd.Parameters.Add("@cmpid", SqlDbType.Int).Value = 22;
Con.Open();
SqlDataAdapter DA = new SqlDataAdapter(cmd);
DA.Fill(DS);
DataTable dTable = DS.Tables[0];
strjson = GetJSONString(dTable);
Con.Close();
return strjson;
}
catch (Exception ex)
{
throw new System.Exception("Error In Get Data" + ex.Message);
}
}
This answer not for experts only for just beginners, thank you.
Upvotes: 0
Reputation: 1799
dont use alert! That will show this only. Since your response is a Json Object, please try to JSON.stringify it or use console.log to view the data
Upvotes: 0
Reputation: 6674
The reason you are seeing that is that data.d
is an object representing the returned JSON text response. When you pass in an object into alert
, it displays [object Object]
. Whatever you are looking for will be a property of data.d
. I would put a breakpoint on that line and see what properties are available. It'll be something like data.d.myProperty
which will contain the actual string / HTML you are trying to work with.
Upvotes: 2