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 data
System.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="bookstore";
}
else{
error();
}
}
}); 大家可以去试试喽!