簡體   English   中英

在 Ajax 響應中從 WebSerice 返回 json 對象

[英]Returning json objects from a WebSerice in an Ajax Response

我正在嘗試將來自 Web 服務(ASP.NET WebForms 應用程序)的 json 對象返回到 Ajax 成功響應中。 盡管 Web 服務中正在返回數據,但沒有數據進入 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>

嘗試返回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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM