C# 从web服务返回json数据
我在ASPXC# 从web服务返回json数据,c#,asp.net,json,C#,Asp.net,Json,我在ASPXtestjson.ASPX上的代码 var app = angular.module('myApp', []); app.controller('customersCtrl', function ($scope, $http) { $http.post("WebService2.asmx/contacters") .then(function (response) { $scope.names = response.data.info;
testjson.ASPX上的代码
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);
});
});
{{x.AuthorId}
{{x.Fname}
{{x.Lname}
调试webservice时,webservice正在显示数据
但问题是当我运行页面testjson.aspx
时,数据没有显示
有人能帮我吗
web服务运行良好,我认为可能存在问题
我的JS文件中存在问题
您的响应已序列化,因此需要在脚本中进行解析,请尝试以下操作
<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>
您还可以使用静态方法以字符串类型返回web服务,如下面的代码所示
JSON.parse(data);
[WebMethod]
公共静态串接触器()
{
SqlConnection con=新的SqlConnection(ConnectionString);
List obj=新列表();
SqlCommand cmd=newsqlcommand(“从作者中选择*”,con);
SqlDataAdapter da=新的SqlDataAdapter(cmd);
数据集ds=新数据集();
da.填充(ds);
对于(int i=0;i请尝试以下方法:
[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;
}
公共类联系人模型
{
公共int AuthorId{get;set;}
公共字符串Fname{get;set;}
公共字符串Lname{get;set;}
}
[网络方法]
公众联络人()
{
SqlConnection con=新的SqlConnection(ConnectionString);
List obj=新列表();
SqlCommand cmd=newsqlcommand(“从作者中选择*”,con);
SqlDataAdapter da=新的SqlDataAdapter(cmd);
数据集ds=新数据集();
da.填充(ds);
对于(int i=0;i
尝试调试JS函数。检查“response.data.info;”的内容。看起来您发送的JSON无效。使用:而不是;@OrkhanAlikhanov(在JS或webservice中?@IbrahimShaikh(在webservice中)。将其设置为Context.response.Write(“{”+“+”信息“+”+”+“+”:“+JSON+”)
@OrkhanAlikhanov什么都没发生,我认为问题不在于webservice,而在于javascript fil或aspx pageJSON.parse(数据);
我需要在何处传递此信息?$scope.names=response.data.info;在此之前,您必须解析此web服务用作其默认命名空间的Json数据。建议:在XML web服务公开之前更改默认命名空间。您需要检查Json响应格式。Json格式似乎有问题,但仍然无法获得任何信息要测试响应,我正在使用此扩展
[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;
}
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);
}