[关闭]
@bergus 2015-12-02T14:30:20.000000Z 字数 1509 阅读 1532

修改jquery的remote让前段显示服务器错误信息

javascript jquery 显示服务器错误信息


  1. remote: function (value, element, param) {
  2. //param 是你的rules中规定的参数,在这里肯定是remote中的参数了
  3. //value是你输入的值
  4. //element是你操作的元素,标签
  5. if (this.optional(element)) {
  6. return "dependency-mismatch";
  7. }
  8. var previous = this.previousValue(element),
  9. validator, data;
  10. if (!this.settings.messages[element.name]) {
  11. this.settings.messages[element.name] = {};
  12. }
  13. previous.originalMessage = this.settings.messages[element.name].remote;
  14. this.settings.messages[element.name].remote = previous.message;
  15. param = typeof param === "string" && {url: param} || param;
  16. if (previous.old !== value) {
  17. previous.old = value;
  18. validator = this;
  19. this.startRequest(element);
  20. data = {};
  21. data[element.name] = value;//输入框中的数据
  22. $.ajax($.extend(true, {
  23. url: param,
  24. mode: "abort",
  25. port: "validate" + element.name,
  26. dataType: "json",
  27. data: data,
  28. context: validator.currentForm,
  29. success: function (response) {
  30. var valid = response['value'] === true || response['value'] === "true", errors, message, submitted;
  31. validator.settings.messages[element.name].remote = previous.originalMessage;
  32. if (valid) {
  33. submitted = validator.formSubmitted;
  34. validator.prepareElement(element);
  35. validator.formSubmitted = submitted;
  36. validator.successList.push(element);
  37. delete validator.invalid[element.name];
  38. validator.showErrors();
  39. } else {
  40. errors = {};
  41. message = response['msg'] || validator.defaultMessage(element, "remote");
  42. errors[element.name] = previous.message = $.isFunction(message) ? message(value) : message;
  43. validator.invalid[element.name] = true;
  44. validator.showErrors(errors);
  45. }
  46. previous.valid = valid;
  47. validator.stopRequest(element, valid);
  48. }
  49. }, param));
  50. return "pending";
  51. }
  52. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注