簡體   English   中英

從Jquery對Servlet的Ajax調用

[英]Ajax call to servlet from Jquery

我正在嘗試使用Jquery .ajax方法訪問servlet,如下所示。

function ajaxCall()
    {
     $.ajax({
        type: 'GET',
        dataType:"html",
        url: "http://localhost:7001/Macaw/MacawServlet",
        success:function(data){
         alert(data);
        },
    error:function(){
         alert("failure");
        }                   
    });
    }

Servlet內容:

   package servlet;

   import java.io.File;
   import java.io.IOException;
   import java.io.PrintWriter;

   import javax.servlet.ServletConfig;
   import javax.servlet.ServletException;
   import javax.servlet.http.HttpServlet;
   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpServletResponse;

   import org.jsoup.Jsoup;
   import org.jsoup.nodes.Document;
   import org.jsoup.nodes.Element;


   public class MacawServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

       public MacawServlet() {
           super();
           // TODO Auto-generated constructor stub
       }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        File input = new File("C:/Users/540893/workspace/Macaw/WebContent/prpsl_txtsrch.html");
        Document doc = Jsoup.parse(input,"UTF-8");
        Element content = doc.getElementById("text_search");
        Element content1 = doc.getElementById("chr_val_stats");
        Element content2 = doc.getElementById("chr_val_delta");
        out.println(content);
        out.println(content1);
        out.println(content2);
        System.out.println("success");
    }

   }

每次我調用此.ajax函數時,它都會訪問servlet並調用.ajax error:function(){}

為什么不調用.ajax success:function(){}

您的js和Java文件是否在同一上下文中?

如果是,則無需使用URL中的服務器地址和端口。 您可以從/ Macaw / MacawServlet開始

如果不是,請選中啟用跨瀏覽器Ajax調用。

參考-Firefox設置以啟用跨域Ajax請求

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM