//--------- Java Socket Server --------
package com.company.server;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.net.Socket;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Worker implements Runnable {
private static Logger logger = LoggerFactory.getLogger(Worker.class);
private Socket socket;
public Worker(final Socket socketParam) {
socket = socketParam;
}
public void run() {
logger.trace(String.format("Connection established with %s.", socket.getRemoteSocketAddress()));
orchestrate();
}
public void orchestrate() {
try {
final HttpPacket httpIn = readRequest();
respond(httpIn);
} catch (IOException e) {
e.printStackTrace();
}
}
public HttpPacket readRequest() throws IOException {
final HttpPacket httpIn = new HttpPacket();
final BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
byte[] streamBuf = new byte[in.available()];
in.read(streamBuf);
final String payload = new String(streamBuf, "UTF-8");
final String[] payloadSplit = payload.split(System.getProperty("line.separator"));
final StringBuilder headerBuilder = new StringBuilder();
final StringBuilder bodyBuilder = new StringBuilder();
boolean bodyField = false;
for (int i = 0; i< payloadSplit.length; i++) {
if (i == 0) {
String[] firstLine = payloadSplit[0].split("\\s");
httpIn.setMethod(firstLine[0]);
httpIn.setUrl(firstLine[1]);
} else if ("".equals(payloadSplit[i].trim())) {
bodyField = true;
} else if (bodyField) {
bodyBuilder.append(payloadSplit[i]);
bodyBuilder.append("\n");
} else {
headerBuilder.append(payloadSplit[i]);
headerBuilder.append("\n");
}
}
final Properties props = parsePropertiesString(headerBuilder.toString());
httpIn.setHttpHeaders(props);
httpIn.setBody(bodyBuilder.toString());
logger.trace(String.format("Data received from from %s connection:\n%s",
socket.getRemoteSocketAddress(), payload));
return httpIn;
}
public Properties parsePropertiesString(String s) throws IOException {
final Properties p = new Properties();
p.load(new StringReader(s));
return p;
}
public void respond(final HttpPacket httpIn) throws IOException {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
StringBuilder builder = new StringBuilder();
builder.append("HTTP/1.1 200 OK" + System.getProperty("line.separator"));
builder.append("Content-Type: text/plain" + System.getProperty("line.separator"));
builder.append("Content-Length: " + httpIn.getBody().length()
+ System.getProperty("line.separator") + System.getProperty("line.separator"));
builder.append(httpIn.getBody());
builder.append(httpIn.getBody());
out.write(builder.toString());
out.close();
socket.close();
}
}
//-------------- JQuery Ajax --------------
$(document).ready(function() {
$(".test").click(function() {
testMessage();
});
});
function testMessage() {
var url = "http://localhost/";
$.ajax({
type : 'POST',
url : url ,
contentType: "text/html",
success : function(response, textStatus, jqXHR) {
alert(response);
},
error : function(jqXHR, textStatus, errorThrown) {
alert("Error getting response.");
}
});
};
- 意图 - 当我点击一个按钮(类=&#34;测试&#34;在带有此js的html页面中)时,它应该执行ajax调用并获得响应。
- 问题 - 我没有得到这个ajax调用的响应体。当我使用firebug和mizilla firefox时,我看到的只是&#34; POST http://localhost.uri/ 200 OK -20ms&#34; (忘了.uri - 满足这个编辑器)红色的空体和错误信息(ajax错误函数()被调用)在chrome(没有使用firebug)。
- HttpPacket是应用程序内部的数据结构,仅用于存储传入数据。
醇>