在 Ajax 响应中从 WebSerice 返回 json 对象
Returning json objects from a WebSerice in an Ajax Response
我正在尝试将 Web 服务(ASP.NET WebForms 应用程序)中的 return json 对象转换为 Ajax 成功响应。尽管正在 Web 服务中 return 编辑数据,但没有数据进入 Ajax 响应。
这是网络服务:
[WebMethod]
public List<Images> GetImageObj()
{
List<Images> all = new List<Images>();
Images infoObjs = new Images();
ImageLoading iload = new ImageLoading();
DataTable dtImages = iload.GetData();
foreach (DataRow row in dtImages.Rows)
{
infoObjs.ID = Convert.ToInt32(row["FILE_ID"].ToString());
Byte[] bytes = ((Byte[])row["THUMBNAIL_CONTENT"]);
infoObjs.ImageURI = iload.GetImage(bytes);
all.Add(infoObjs);
}
return all;
}
public class Images
{
public List<Images> imgObjs;
public string ImageURI;
public int ID;
}
这里是 Ajax 回调函数:
function AjaxCall() {
$(document).ready(function () {
var dataArr = [];
$.ajax({
url: '../WebService1.asmx/GetImageObj',
method: 'post',
contentType: 'application/json;charset=utf-8',
dataType: 'json',
data: "{'aray':" + JSON.stringify(dataArr) + "}",
success: function (response) {
//No data is making it here:
var getData = JSON.parse(response.d);
alert(getData);
},
error: function (err) {
alert(err);
}
});
});
}
更新:@Julian 的回答解决了我的问题。但是,我必须将以下内容添加到我的 webconfig 中以容纳来自编码图像 URI 的大数据:
<system.web.extensions>
<scripting>
<webServices>
<!-- Update this value to change the value to a larger value that can accommodate your JSON Strings -->
<jsonSerialization maxJsonLength="86753090" />
</webServices>
</scripting>
</system.web.extensions>
尝试 return 一个 string
,但使用 Json.NewtonSoft
库序列化 JSON 中的对象。
还要在要从 Ajax 使用的服务上添加 [ScriptService]
标签:
C#代码:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
[WebMethod]
public string GetImageObj()
{
List<Images> all = new List<Images>();
Images infoObjs = new Images();
ImageLoading iload = new ImageLoading();
DataTable dtImages = iload.GetData();
foreach (DataRow row in dtImages.Rows)
{
infoObjs.ID = Convert.ToInt32(row["FILE_ID"].ToString());
Byte[] bytes = ((Byte[])row["THUMBNAIL_CONTENT"]);
infoObjs.ImageURI = iload.GetImage(bytes);
all.Add(infoObjs);
}
return JsonConvert.SerializeObject(all, Formatting.Indented);
}
}
AJAX代码:
<script type="text/javascript">
$(document).ready(function () {
var dataArr = [];
$.ajax({
url: '../WebService.asmx/GetImageObj',
method: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: "{}",
success: function (response) {
//No data is making it here:
var getData = JSON.parse(response.d);
console.log(getData);
},
error: function (err) {
console.log(err);
}
});
});
</script>
我正在尝试将 Web 服务(ASP.NET WebForms 应用程序)中的 return json 对象转换为 Ajax 成功响应。尽管正在 Web 服务中 return 编辑数据,但没有数据进入 Ajax 响应。
这是网络服务:
[WebMethod]
public List<Images> GetImageObj()
{
List<Images> all = new List<Images>();
Images infoObjs = new Images();
ImageLoading iload = new ImageLoading();
DataTable dtImages = iload.GetData();
foreach (DataRow row in dtImages.Rows)
{
infoObjs.ID = Convert.ToInt32(row["FILE_ID"].ToString());
Byte[] bytes = ((Byte[])row["THUMBNAIL_CONTENT"]);
infoObjs.ImageURI = iload.GetImage(bytes);
all.Add(infoObjs);
}
return all;
}
public class Images
{
public List<Images> imgObjs;
public string ImageURI;
public int ID;
}
这里是 Ajax 回调函数:
function AjaxCall() {
$(document).ready(function () {
var dataArr = [];
$.ajax({
url: '../WebService1.asmx/GetImageObj',
method: 'post',
contentType: 'application/json;charset=utf-8',
dataType: 'json',
data: "{'aray':" + JSON.stringify(dataArr) + "}",
success: function (response) {
//No data is making it here:
var getData = JSON.parse(response.d);
alert(getData);
},
error: function (err) {
alert(err);
}
});
});
}
更新:@Julian 的回答解决了我的问题。但是,我必须将以下内容添加到我的 webconfig 中以容纳来自编码图像 URI 的大数据:
<system.web.extensions>
<scripting>
<webServices>
<!-- Update this value to change the value to a larger value that can accommodate your JSON Strings -->
<jsonSerialization maxJsonLength="86753090" />
</webServices>
</scripting>
</system.web.extensions>
尝试 return 一个 string
,但使用 Json.NewtonSoft
库序列化 JSON 中的对象。
还要在要从 Ajax 使用的服务上添加 [ScriptService]
标签:
C#代码:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
[WebMethod]
public string GetImageObj()
{
List<Images> all = new List<Images>();
Images infoObjs = new Images();
ImageLoading iload = new ImageLoading();
DataTable dtImages = iload.GetData();
foreach (DataRow row in dtImages.Rows)
{
infoObjs.ID = Convert.ToInt32(row["FILE_ID"].ToString());
Byte[] bytes = ((Byte[])row["THUMBNAIL_CONTENT"]);
infoObjs.ImageURI = iload.GetImage(bytes);
all.Add(infoObjs);
}
return JsonConvert.SerializeObject(all, Formatting.Indented);
}
}
AJAX代码:
<script type="text/javascript">
$(document).ready(function () {
var dataArr = [];
$.ajax({
url: '../WebService.asmx/GetImageObj',
method: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: "{}",
success: function (response) {
//No data is making it here:
var getData = JSON.parse(response.d);
console.log(getData);
},
error: function (err) {
console.log(err);
}
});
});
</script>