2
 [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

asked Sep 23, 2016 at 6:30
16
  • Try debuggin the JS function. Check the content of "response.data.info;". Commented Sep 23, 2016 at 6:38
  • It looks like you are sending invalid JSON. Use : instead of ; Commented Sep 23, 2016 at 6:43
  • @OrkhanAlikhanov in js or in webservice? Commented Sep 23, 2016 at 6:53
  • @IbrahimShaikh In WebService. Make it Context.Response.Write("{"+'"'+"info"+'"'+":"+json+"}"); Commented Sep 23, 2016 at 6:53
  • @OrkhanAlikhanov nothing happend, And I think the problem is not with webservice, the problem is with javascript fil or with aspx page Commented Sep 23, 2016 at 6:57

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

still not getting anything
Use this to test the response
I am using this extension
1

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

6 Comments

JSON.parse(data); where i need to pass this?
$scope.names = response.data.info;
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.
You need to check your json response format.seem issue with json format
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.