jQuery Ajax 请求出现 404 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15145929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
404 error with Ajax request
提问by Daniel Swater
I need to grab some data from a JSP page that does a select on a database and then put inside a div. I need to do this with ajax.
我需要从对数据库进行选择的 JSP 页面中获取一些数据,然后将其放入 div。我需要用ajax来做到这一点。
Here is my code:
这是我的代码:
$(function() {
teste();
});
function teste() {
var v1 = document.getElementById("selCodigo").value;
alert(v1);
$.ajax({
type : "GET",
data : "turma="+v1,
url : "busca-notas.jsp",
success : function(resposta){
alert("DEU CERTO");
},
error : function(xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
document.getElementById("notas").innerHTML = "ERRO";
}
});
}
I tested the variable v1
and the value that it receives necessary, and in my JSP page, I do this:
我测试了变量v1
和它接收到的必要值,在我的 JSP 页面中,我这样做:
String turmaSelecionada = request.getParameter("turma");
the problem is that the ajax content that does not feed into the div need, beyond what the xhr.status
presents thrownError and a 404 error not found
问题是不提供给 div 的 ajax 内容需要超出所xhr.status
呈现的 throwError 和 404 错误未找到
Can anyone help me?
谁能帮我?
回答by Hazzit
Either, busca-notas.jsp
does not exist, or it is on a different server or path as the HTML calling the Ajax request.
要么busca-notas.jsp
不存在,要么与调用 Ajax 请求的 HTML 位于不同的服务器或路径上。
Example: If your HTML and JavaScript is here:
示例:如果您的 HTML 和 JavaScript 在这里:
http://www.example.com/somepath/page.html
and your PHP code is here:
你的 PHP 代码在这里:
http://www.example.com/otherpath/busca-notas.jsp
then you'll Need to use url: "../otherpath/busca-notas.jps"
. There is an easy way to check: Open your HTML in the browser, remove the last bit of the path, and replace it with "busca-notas.jpg", and see what you're getting.
那么你将需要使用url: "../otherpath/busca-notas.jps"
. 有一个简单的检查方法:在浏览器中打开您的 HTML,删除路径的最后一位,并将其替换为“busca-notas.jpg”,然后看看您得到了什么。
A 404 also means, your JSP code never gets executed.
404 还意味着,您的 JSP 代码永远不会被执行。
回答by JBone
This is saying the resource you are trying to do a GET to is not there. The path you are doing a GET to is probably incorrect. Can you tell the structure of your files (javascript/service files etc...). I would suggest using the browser developer tools or fiddler to debug what is going on.
这是说您尝试执行 GET 的资源不存在。您正在执行 GET 的路径可能不正确。你能告诉你文件的结构吗(javascript/服务文件等......)。我建议使用浏览器开发人员工具或 fiddler 来调试正在发生的事情。
Use F12 (windows) with browsers to get to the developer tools. Also the fiddler tool is great! http://www.fiddler2.com/fiddler2/
在浏览器中使用 F12 (windows) 以访问开发人员工具。提琴手工具也很棒!http://www.fiddler2.com/fiddler2/
On a side note if you use console.log for debugging you will never go back to alerts :)
附带说明一下,如果您使用 console.log 进行调试,您将永远不会回到警报:)