[WebMethod]
public void contacters()
{
SqlConnection con = new SqlConnection(ConnectionString);
List<object> obj = new List<object>();
SqlCommand cmd = new SqlCommand("select * from authors", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
for(int i=0; i<ds.Tables[0].Rows.Count; i++)
{
obj.Add(ds.Tables[0].Rows[i][0]);
obj.Add(ds.Tables[0].Rows[i][1]);
obj.Add(ds.Tables[0].Rows[i][2]);
}
JavaScriptSerializer ser = new JavaScriptSerializer();
var json = ser.Serialize(obj);
Context.Response.Write("{"+'"'+"info"+'"'+";"+json+"}");
}
This Is my Web service
And My JS File
var app = angular.module('myApp', []);
app.controller('customersCtrl', function ($scope, $http) {
$http.post("WebService2.asmx/contacters")
.then(function (response) {
$scope.names = response.data.info;
console.log(response.data.info);
});
});
My Code On ASPX testjson.aspx
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div ng-app="myApp" ng-controller="customersCtrl">
<table>
<tr ng-repeat="x in names">
<td>{{ x.AuthorId }}</td>
<td>{{ x.Fname }}</td>
<td>{{ x.Lname }}</td>
</tr>
</table>
</div>
<script type="text/javascript" src="testjavascript.js"></script>
</asp:Content>
webservice is displaying data when i debug webservice,
But the problem is when i run the page testjson.aspx data is not displaying
Can anybody help me out on this
web service is running fine, I think there may be a
problem in my JS file
2 Answers 2
Try this:
public class ContactModel
{
public int AuthorId { get; set; }
public string Fname { get; set; }
public string Lname { get; set; }
}
[WebMethod]
public void contacters()
{
SqlConnection con = new SqlConnection(ConnectionString);
List<ContactModel> obj = new List<ContactModel>();
SqlCommand cmd = new SqlCommand("select * from authors", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
obj.Add(new ContactModel
{
AuthorId = (int) ds.Tables[0].Rows[i][0],
Fname = (string) ds.Tables[0].Rows[i][1],
Lname = (string) ds.Tables[0].Rows[i][2]
});
}
JavaScriptSerializer ser = new JavaScriptSerializer();
var returnModel = new { info = obj };
var json = ser.Serialize(returnModel);
Context.Response.Write(json);
}
answered Sep 23, 2016 at 7:10
Orkhan Alikhanov
10.2k4 gold badges50 silver badges66 bronze badges
Sign up to request clarification or add additional context in comments.
you response is serialized so you need to parse in your script, try this
JSON.parse(data);
And also you can return web service as string type with static method like below code.
[WebMethod]
public static string contacters()
{
SqlConnection con = new SqlConnection(ConnectionString);
List<object> obj = new List<object>();
SqlCommand cmd = new SqlCommand("select * from authors", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
for(int i=0; i<ds.Tables[0].Rows.Count; i++)
{
obj.Add(ds.Tables[0].Rows[i][0]);
obj.Add(ds.Tables[0].Rows[i][1]);
obj.Add(ds.Tables[0].Rows[i][2]);
}enter code here
JavaScriptSerializer ser = new JavaScriptSerializer();
var json = ser.Serialize(obj);
return json;
}
answered Sep 23, 2016 at 7:05
Bharat
2,4643 gold badges28 silver badges37 bronze badges
6 Comments
Bharat
$scope.names = response.data.info;
Bharat
before that you have to pars Json data
This web service is using tempuri.org as its default namespace. Recommendation: Change the default namespace before the XML Web service is made public.
Bharat
You need to check your json response format.seem issue with json format
|
lang-cs
Context.Response.Write("{"+'"'+"info"+'"'+":"+json+"}");