JSON请求的动态脚本标记...检测是否存在XXX错误?

raw*_*rrr 5 javascript scripting json dynamic

我用动态脚本标签做了一堆json请求.是否有可能检测到请求中是否存在错误(例如503错误,404错误)并在检测到错误时运行某些操作?

Mar*_*ius 11

改用ajax.AFAIK无法检测脚本标记是否加载,如果没有,为什么它没有加载.使用ajax你可以加载json,它会告诉你为什么它没有加载.

使用像jQuery这样的库变得非常简单:

$.ajax({
  type: "GET",
  url: "test.js",
  dataType: "script",
  error: function(xhr, error, exception){
    alert(xhr.status); //Will alert 404 if the script does not exist
  }
});
Run Code Online (Sandbox Code Playgroud)