:用 java 发送 ajax 请求怎么实现刷票呢?希望下面的文章对大家有所帮助。
- import java.io.IOException;
- import org.apache.commons.httpclient.HttpClient;
- import org.apache.commons.httpclient.HttpException;
- import org.apache.commons.httpclient.HttpStatus;
- import org.apache.commons.httpclient.methods.PostMethod;
- public class AjaxSend {
- private static String url = "http://172.26.188.112:8080/BooksStore/ajax/selectuser";
- public static void main(String[] args) {
- HttpClient client = new HttpClient();
- String body = "[{\"name\":liming,\"password\":123456}]";
- method(client, url, body);
- }@SuppressWarnings("deprecation") public static void method(HttpClient client, String url, String body) {
- PostMethod method = new PostMethod(url);
- method.setRequestBody(body); // addParameters(postData);try {// Execute the method.int statusCode = client.executeMethod(method);if (statusCode != HttpStatus.SC_OK) {System.err.println("Method failed: " + method.getStatusLine());}// Read the response body.byte[] responseBody = method.getResponseBody();// Deal with the response.// Use caution: ensure correct character encoding and is not binary dataSystem.out.println(new String(responseBody, "utf-8"));}catch (HttpException e) {System.err.println("Fatal protocol violation: " + e.getMessage());e.printStackTrace();}catch (IOException e) {System.err.println("Fatal transport error: " + e.getMessage());e.printStackTrace();}finally {// Release the connection.method.releaseConnection();}}}
我在前台捕捉到的 ajax 是这样的:
- $.ajax({
- type: "POST",
- url: ContextPath + urls.getXxl,
- data: {
- name: $("#user").val(),
- password: $("#password").val()
- },
- dataType: "json",
- async: false,
- cache: false,
- success: function(jsonData) {
- if (jsonData.dataExit) {
- window.location.href = "xxx";
- } else {
- error();
- }
- }
- });
就爱阅读 www.92to.com 网友整理上传, 为您提供最全的知识大全, 期待您的分享,转载请注明出处。
来源: http://www.92to.com/bangong/2017/04-19/20639392.html