繁体   English   中英

通过JavaScript进行Ajax调用

[英]Making Ajax call from javascript

我有以下jquery ajax调用,它在纯jquery文件中可以正常工作。

var request = $.ajax({
    url: "kscript.jsp",
    type: "POST",
    data: {st:start, sp:stop},
    dataType: "html"
});
request.done(function(msg) {
    $("#output").html( msg );
    alert("Success!!!"+msg);
});
request.fail(function(jqXHR, textStatus) {
    alert( "Request failed: " + textStatus );
});

此后,我将代码重写为javascript,但现在将ajax调用直接放在javascript函数中。 这没有用,并且我收到500 Internal Server Error。

function myAjax(){
    var request = $.ajax({
        url: "kscript.jsp",
        type: "POST",
        data: {st:start, sp:stop},
        dataType: "html"
    });
    request.done(function(msg) {
        $("#output").html( msg );
        alert("Success!!!"+msg);
    });
    request.fail(function(jqXHR, textStatus) {
        alert( "Request failed: " + textStatus );
    });
}

我也尝试过这个:

function ajaxFunction() {
xmlhttp.open("POST","kscript.jsp",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("st=start&sp=stop");

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("output").innerHTML=xmlhttp.responseText;
    }
  }

}

但相同的错误:500 Internal Server Error。 在所有这些情况下,错误都指向kscript.jsp。我确保URL和拼写正确,但没有用。 感谢您提出的解决此问题的建议。

这是kscript.jsp

    <%

        String astart = request.getParameter("start");
        String sptimes=request.getParameter("stop");

        out.print("<h1> Start is: "+start+"    --  Stop is"+stop +"</h1>");

    %>

它看起来像是文件夹结构问题,应确保相对路径正确,我的意思是,如果您是从js调用的,则应指向/jsp/yourJsp.jsp,请使用checkit ...顺便说一句,您是否在使用使用F12工具验证服务器的响应?

我认为您在调用Java脚本method.so时犯了一些错误,请检查或在javascript标签中描述ajaxcall。

您正在尝试访问jsp中的错误参数。

错误发生在这里:

String astart = request.getParameter("start");

参数是stsp ,而不是startstop

你需要:

String astart = request.getParameter("st");
String sptimes=request.getParameter("sp");

或者,您可以更改js以传递正确的参数:

data: {start:start, stop:stop},

暂无
暂无

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

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