我是一个JQuery n00b.我正在尝试使用$ .get()编写一个非常简单的代码.在官方文件说:
If a request with jQuery.get() returns an error code, it will fail silently unless the script has also called the global .ajaxError() method or. As of jQuery 1.5, the .error() method of the jqXHR object returned by jQuery.get() is also available for error handling.
所以,如果一切顺利,我将调用成功的回调函数.但是,如果请求失败,我想获取HTTP代码:404,502等,并为用户制定有意义的错误消息.
但是,由于这是一个异步调用,我可以想象我可能有几个优秀..ajaxError()如何知道它对应哪个请求?也许最好使用jQuery.get()返回的jqXHR对象的.error()方法?
有人可以请一个非常简单的代码示例吗?也许成功例程调用Alert("找到页面")和失败例程检查404并执行警报("找不到页面")
更新:以下页面非常有用... http://api.jquery.com/jQuery.get/
我将要描述的问题与我已经发现的问题非常相似(例如这篇文章名称几乎相同),但我希望我能把它变成一个不重复的东西.
我在Visual Studio中创建了一个新的ASP.NET MVC 5应用程序.然后,我定义了两个模型类:
public class SearchCriterionModel
{
public string Keyword { get; set; }
}
public class SearchResultModel
{
public int Id { get; set; }
public string FirstName { get; set; }
public string Surname { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后我创建了SearchController如下:
public class SearchController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult DisplaySearchResults()
{
var model = new List<SearchResultModel>
{
new SearchResultModel { Id=1, FirstName="Peter", Surname="Pan" },
new SearchResultModel …Run Code Online (Sandbox Code Playgroud)