【问题标题】:Jquery Ajax function is not working while calling the Servlet调用 Servlet 时 Jquery Ajax 函数不起作用
【发布时间】:2015-08-27 09:17:53
【问题描述】:

我在调用 ajax 时遇到了一些问题。任何人都可以查看我的代码并提出解决我问题的建议

function search(file,input){
      $.ajax({url:'/Searchandhighlight?name='+input+'&file='+file,type:"post",
              success:function(){
                 $("#bodyy").html();
              }
    });
   }

我正在使用 ajax 调用 servlet,servlet 更改了数据库的一些内容,成功后我尝试刷新我的“#bodyy”div。

提前致谢

【问题讨论】:

  • 什么参数文件保存?
  • 它的文件名类似于“C:\apache-tomcat-7.0.63\webapps\data\Airline Ticketing\Italian VAT\hello.txt”
  • 并且“输入”从文本框中保存字符串
  • 好的...你可以试试下面的代码,让我们知道是否成功打印。

标签: javascript jquery ajax servlets


【解决方案1】:

首先让我们知道文件变量包含什么。

通常带有参数的 Ajax 调用是这样的。 dataType 取决于您期待什么样的回应。

 $.ajax({
                type: "POST",
                url: "/Searchandhighlight",                
                dataType: "json",
                data: {"name" : input, "file" : file},
                success:function(data){
                    if(data){
                        alert("success");
                    }
                },
                error:function(){
                    alert('failed');
                } 

            })   

【讨论】:

  • 它根本不调用 servlet
  • 我试图在 ajax 调用之前打印一些值,但它会卡在 ajax 调用中
  • 请传递硬编码的数据以查看它是否正在调用 servlet。喜欢 {"Name" : "person", "file": "TBD"} 和测试,否则我怀疑 URL 没有指向正确的位置。
  • 嘿,我正在打印一些 hello 字符串以告知 servlet 是否被调用。但它没有打印 hello 字符串,意味着没有调用 Servlet。
  • 好的,请通过此链接,检查您的 servlet 并使其正常工作。 stackoverflow.com/questions/1830533/…
【解决方案2】:

我终于通过以下代码解决了我的问题

    function search(file,input){
      if(input != "") {
       xmlhttp = new XMLHttpRequest();
        xmlhttp.open("POST", "/Searchandhighlight?name=" + input + "&file=" + file, true);
        xmlhttp.onreadystatechange = function () {
          if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            window.location.reload();
          }
       }
        xmlhttp.send(null);
      }
   }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-24
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多