简体   繁体   English

C#Ajax URL返回值,但函数未成功执行

[英]C# Ajax URL returning value, but function not hitting success

The following AJAX code: 以下AJAX代码:

 $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: "{ 'userId' : '" + $('#userId').val() + "' }",
            url: "/User/UserURL",
            success: function (data) {                    
               Do something with data*
            },
            error: function () {
                alert('error');
            }
        });

Is hitting the /User/UserURL function correctly, which is: 正确命中/ User / UserURL函数,即:

 public JsonResult UserURL(int userId)
    {
        List<USER> u = (*Get from database using LINQ*).ToList();
        return Json(u, JsonRequestBehavior.AllowGet);
    }

When I put a debug point and check, the List<USER> u has the required value at the end while returning from the UserURL . 当我放置调试点并进行检查时,从UserURL返回时, List<USER> u在末尾具有所需的值。 However, at coming back to the page from where AJAX is being called, I get the 'error' alert. 但是,返回到调用AJAX的页面时,出现“错误”警报。 Why is it not hitting the success? 为什么没有达到成功?

Edit: If it makes any difference, this is what's going on inside my success: 编辑:如果有什么不同,这就是我成功的过程:

 for (i = 0; i < data.length; i++) {
      $('input[name="checkbox"]').each(function () {
          if (this.value == data[i].USER_ID) {
             (this).checked = true;
          }
      });
  }

It is difficult to help you find a solution to your problem, since the question didn't describe any specific type of error message. 因为问题没有描述任何特定类型的错误消息,所以很难帮助您找到解决问题的方法。

So with what I am about to show you, you might not need any of our help to diagnose the issue. 因此,根据我要向您展示的内容,您可能不需要我们的任何帮助来诊断问题。

Since you say at coming back to the page from where AJAX is being called, I get the 'error' alert , you need to do some more digging into why it is doing that, so this should help. 既然您说过要从调用AJAX的页面返回页面,那么我会收到“错误”警报 ,您需要进一步研究其原因,因此应该有所帮助。

error: function (jqhxr) {
            alert(jqhxr);
        }

error Type: Function( jqXHR jqXHR, String textStatus, String errorThrown ) 错误类型:函数(jqXHR jqXHR,字符串textStatus,字符串errorThrown)

The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object. jqXHR对象(在jQuery 1.4.x中为XMLHttpRequest),一个描述发生的错误类型的字符串和一个可选的异常对象。

Once that is alerted it will help you diagnose what the real problem is. 收到警报后,它将帮助您诊断出真正的问题是什么。

Hope this helps! 希望这可以帮助!

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

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