求助:Ajax请求出现404错误

我需要用ajax从数据库的JSP页面中获取一些数据,然后放入div中。

这是我的代码:

$(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";
       }
    });
}

我测试了变量 v1 及其必需的值,在我的JSP页面中:

String turmaSelecionada = request.getParameter("turma");

问题是,除了 xhr.status 所呈现的thrownError和未发现404错误之外,ajax内容没有馈入div。

有人能帮帮我吗?

Either, busca-notas.jsp does not exist, or it is on a different server or path as the HTML calling the Ajax request.

Example: If your HTML and JavaScript is here:

http://www.example.com/somepath/page.html

and your PHP code is here:

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.

A 404 also means, your JSP code never gets executed.

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.

Use F12 (windows) with browsers to get to the developer tools. Also the fiddler tool is great! http://www.fiddler2.com/fiddler2/

On a side note if you use console.log for debugging you will never go back to alerts :)