简体   繁体   English

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

[英]Javascript/AJAX Error 404 Not found or Undefined

I have a page which loops down the first column and copies the value to a text box, and then it is supposed to query data.asp , but I keep getting an error of either GET http://192.168.1.12/pb_search/v2/demo/data.asp?h_prodref=undefined 404 (Not Found) or XHR finished loading: GET "http://192.168.1.12/pb_search/v2/demo/data.asp?h_prodref=undefined" . 我有一个页面,该页面向下循环第一列并将其值复制到文本框,然后应该查询data.asp ,但是我不断收到GET http://192.168.1.12/pb_search/v2/demo/data.asp?h_prodref=undefined 404 (Not Found)的错误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" Errors from Google Chrome Developer Tools. Google Chrome开发者工具中的错误。

Both my scripts work independently, but when I piece them together, I get these erorors. 我的两个脚本都是独立工作的,但是当我将它们组合在一起时,就会遇到这些错误。 I am probably missing something really simple, but I very much learning this on the fly, so any help would be appreciated. 我可能错过了一些非常简单的东西,但是我在飞行中学习了很多东西,因此我们将不胜感激。

Full Code 完整代码

<%@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>

So What's the Problem? 所以有什么问题?

The value undefined is getting added into AJAX call that is made instead of the expected value of x[i].value . undefined的值将添加到进行的AJAX调用中,而不是x[i].value的期望值。 I'm making an assumption here though and that is that 我在这里做一个假设,那就是

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

exists and the HTTP 404 Not Found is being forced by the data.asp script as a scripted response and not because the server can't find the data.asp page. 存在,并且data.asp脚本将HTTP 404 Not Found作为脚本响应强制执行,而不是因为服务器找不到data.asp页面。

Restructuring the JavaScript 重组JavaScript

When calling a function you don't need the entire definition at the point where you want to call it, if this was the case you would have the same function being duplicated throughout the code where it is called breaking fundamental principles in programming like DRY . 调用function时,不需要在要调用它的地方定义整个定义,如果是这种情况,您将在整个代码中复制相同的函数,这被称为打破DRY之类的编程基本原理。

Here is a quick example of restructuring the JavaScript code: 这是重组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();
}

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

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