找不到Javascript / AJAX错误404或未定义

乔纳森·格里芬(Jonathan Griffin)

我有一个页面,该页面向下循环第一列,然后将值复制到文本框,然后应该进行查询data.asp,但是我不断收到GET http://192.168.1.12/pb_search/v2/demo/data.asp?h_prodref=undefined 404 (Not Found)的错误XHR finished loading: GET "http://192.168.1.12/pb_search/v2/demo/data.asp?h_prodref=undefined"Google Chrome开发者工具中的错误。

我的两个脚本都是独立工作的,但是当我将它们组合在一起时,就会遇到这些错误。我可能会错过一些非常简单的东西,但是我会即时学习很多东西,因此我们将不胜感激。

完整代码

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<table width="50%" border="0" cellspacing="2" cellpadding="2">
  <tr>
    <td width="16%" class="prodref">84PS01</td>
    <td width="51%"><input type="text" class="h_prodref" /><button type="button" onclick="loadDoc()">Change Content</button></td>
    <td width="33%" id="demo">&nbsp;</td>
  </tr>
  <tr>
    <td class="prodref">92K002</td>
    <td><input type="text" class="h_prodref" /><button type="button" onclick="loadDoc()">Change Content</button></td>
    <td id="demo">&nbsp;</td>
  </tr>
  <tr>
    <td class="prodref">68F017</td>
    <td><input type="text" class="h_prodref" /><button type="button" onclick="loadDoc()">Change Content</button></td>
    <td id="demo">&nbsp;</td>
  </tr>
</table>
<script>
    var prodref = document.getElementsByClassName("prodref");
    var h_prodref = document.getElementsByClassName("h_prodref");
    var i = 0;
    for (i; i < prodref.length; i++) {
    h_prodref[i].value = prodref[i].innerHTML;
function loadDoc() {
    var x = document.getElementsByClassName("h_prodref");
    x[i] = document.getElementsByClassName("h_prodref").value;
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
      document.getElementById("demo").innerHTML = xhttp.responseText;
    }
  }
  xhttp.open("GET", "data.asp?h_prodref="+x[i].value, true);
  xhttp.send();
}
    }
</script>
用户名

所以有什么问题?

该值undefined将添加到进行的AJAX调用中,而不是的预期值x[i].value我在这里做一个假设,那就是

http://192.168.1.12/pb_search/v2/demo/data.asp

存在,并且HTTP 404 Not Founddata.asp脚本强制作为脚本响应,而不是因为服务器找不到data.asp页面。

重组JavaScript

调用a时,function您不需要在要调用它的地方定义整个定义,如果是这种情况,您将在整个代码中复制相同的函数,这被称为打破DRY之类的编程基本原理

这是重组JavaScript代码的快速示例:

var prodref = document.getElementsByClassName("prodref");
var h_prodref = document.getElementsByClassName("h_prodref");
var i = 0;
for (i; i < prodref.length; i++) {
  h_prodref[i].value = prodref[i].innerHTML;
  // Call function inside the loop
  loadDoc();
}

// Definition should be defined once
function loadDoc() {
  var x = document.getElementsByClassName("h_prodref");
  x[i] = document.getElementsByClassName("h_prodref").value;
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
      document.getElementById("demo").innerHTML = xhttp.responseText;
    }
  }
  xhttp.open("GET", "data.asp?h_prodref="+x[i].value, true);
  xhttp.send();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章