包含ajaxjava的詞條

本文目錄一覽:

JAVA的ajax方法?

//方式一:使用post請求的方式,不通過url傳參,採用data傳參

$.ajax({

url:”userAction_findMangerByDeptId”,//訪問的地址

type:”post”,

data:{“傳遞到後台的參數名”:參數},

dataType:’text’,//後台返回的數據格式類型

success:function(data){

})

}

})

}

//方式二:這是get提交方法,會出現中文亂碼,所以要 1.先將數據轉碼

參數名 = encodeURI(encodeURI(要轉的參數));

$.ajax({

url:”userAction_findMangerByDeptId?參數名”+參數值,

type:”get”,

dataType:’text’,//後台返回的數據格式類型

success:function(data){

})

}

})

}

2.後台接收前端傳遞內容後要進行解碼

String 參數名 = URLDecoder.decode(接收的參數);

如何在Java項目中使用Ajax?

如果要使用ajax,必須要導入相應的jquery.js等才可以用。具體用法如下:

$.ajax({

type: “GET”,

url: “test.json”,

data: {username:$(“#username”).val(), content:$(“#content”).val()},

dataType: “json”,

success: function(data){

alert(data.status);

});

});

ajax技術適用於在頁面不刷新的情況下前後台數據的動態數據交互。

如何使用ajax調用java類

ajax調用java後台的方法,其實是通過url鏈接來訪問,示例如下:package com.xxxx.xxxx.servlet;

import java.io.IOException;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.sql.DataSource;

public class oaLoginLimitedServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

private static Connection conn=null;

private static PreparedStatement pstmt=null;

public oaLoginLimitedServlet() {

super();

}

public void destroy() {

super.destroy();

}

public static String getCount(String userid)

{

String v_sql=”…..”;

String v_count=””;

try {

pstmt = conn.prepareStatement(v_sql);

pstmt.setString(1, userid);

ResultSet rs = pstmt.executeQuery();

while(rs.next()){

v_count = rs.getString(1);

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

try {

pstmt.close();

conn.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return v_count;

}

public static Connection getConnection(){

Context ctx = null;

try {

ctx = new InitialContext();

DataSource ds = (DataSource)ctx.lookup(“jndiname”);

conn = ds.getConnection();

} catch (Exception e) {

e.printStackTrace();

}

return conn;

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String v_userid=request.getParameter(“userid”);

System.out.println(v_userid);

getConnection();

String v_count=getCount(v_userid);

response.setCharacterEncoding(“UTF-8”);

response.getWriter().write(v_count);

response.flushBuffer();

}

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doPost(request,response);

}

}

如果要前端能夠訪問到該servlet,需要將該servlet註冊到 web.xml文件中。需要在web.xml文件中添加以下內容

[html] view plaincopy

servlet

servlet-nameoaLoginLimitedServlet/servlet-name

servlet-classcom.xxxx.xxxx.servlet.oaLoginLimitedServlet/servlet-class

/servlet

servlet-mapping

servlet-nameoaLoginLimitedServlet/servlet-name

url-pattern/oaLoginLimitedServlet/url-pattern

/servlet-mapping

重啟相關服務。

通過ajax就可以調用了。

[html] view plaincopy

var msg = $.ajax({

type: “post”,

url: ….+’/oaLoginLimitedServlet?userid=’+ $(‘#act’).val(),

async:false

}).responseText;

原創文章,作者:BKXI,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/134355.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
BKXI的頭像BKXI
上一篇 2024-10-04 00:05
下一篇 2024-10-04 00:05

相關推薦

發表回復

登錄後才能評論