简体   繁体   English

jquery ajax成功回调难度

[英]jquery ajax success callback difficulty

I have the following code: 我有以下代码:

@{
    ViewBag.Title = "Index";
}
<h2>
    Index</h2>
<div data-role="page">
    <div data-role="header">
        ...</div>
    <div data-role="content">
        <a id="btnShowCustomers" data-role="button" href="#secondDiv"></a>
    </div>
    <div data-role="footer">
        ...</div>
</div>
<div id="secondDiv" data-role="page">
    <div id="list" data-role="content">
    </div>
</div>
<div id="customerDetailsDiv" data-role="page">
    <div data-role="content">
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function (event) {
        $('#btnShowCustomers').bind('vclick', function (event) {
            GetCustomers();
        });
    });

    function GetCustomers() {
        var webMethod = "Home/GetCustomers";
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: webMethod,
            data: "{}",
            dataType: "json",
            success: function (dataObj) {
                $(dataObj).each(function () {
                    if ($(this).CanConsume) {
                        alert('can consume');
                        $('<a href="#" data-date="' + $(this).DateActivated + '" data-id="' + $(this).ID + '">' + $(this).Name + '</a>').appendTo('#list');
                    }
                })
            }
        });
    }
</script>

From the server-side I'm returning a list of Customer objects. 从服务器端,我返回一个Customer对象列表。 Customer has the following properties; 客户具有以下特性;

  • ID ID
  • CanConsume (bool) CanConsume(布尔)
  • Name 名称
  • DateActivated DateActivated

I want to iterate through the returned set of results and build anchor elements and append them to a div named list. 我想迭代返回的结果集并构建锚元素并将它们附加到div命名列表中。 Do you have any idea why this is not working? 你知道为什么这不起作用吗? I get no javascript errors. 我没有javascript错误。

dataObj is a normal list of plain JavaScript (JSON) objects. dataObj是普通JavaScript(JSON)对象的常规列表。 Don't use jQuery to iterate over that as jQuery.each(...) iterates over DOM elements. 不要使用jQuery迭代它,因为jQuery.each(...)迭代DOM元素。

Just try 你试一试

   ...
   success: function(dataObj){
      for(var i=0; i < dataObj.length; i++){
         var obj = dataObj[i];
          if (obj.CanConsume) {
              alert('can consume');
              ...
          }
      }
   }

That should work. 这应该工作。

Otherwise try Firebug or the Chrome Dev Tools to inspect the response or to set a breakpoint in your success callback. 否则,请尝试使用Firebug或Chrome开发工具来检查响应或在成功回调中设置断点。 That should give you enough information to fix the problem. 这应该给你足够的信息来解决问题。

jQuery kills errors... unfortunately. 不幸的是,jQuery杀死了错误。

If that does not work, please post some JSON. 如果这不起作用,请发布一些JSON。

damn to quick, again: 该死的,再次:

$.each(dataObj, function (index, element) {
    if (element.CanConsume) {
           alert('can consume');
           $('<a href="#" data-date="' + element.DateActivated + '" data-id="' + element.ID + '">' + element.Name + '</a>').appendTo('#list');
       }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM