我有一個vert.x服務器和一個html頁面,用ajax發送一個post請求。我設法發送帖子請求,但我不明白如何從服務器獲得答案。我的代碼:如何將數據從vert.x服務器發送到js腳本?
$.ajax({
type: 'POST',
url: 'http://localhost:9999',
data: {d:"hi"}
}).done(function(data) {alert("succes " + data);})
.fail(function(data) { alert("fail");})
而vert.x情況如下:
httpServer.requestHandler(new Handler<HttpServerRequest>() {
@Override
public void handle(HttpServerRequest request) {
System.out.println("incoming request!");
if(request.method() == HttpMethod.POST){
//here i want to do: send to the html page some data
//like "hi"
}
}
});
在這個環節http://tutorials.jenkov.com/vert.x/http-server.html中,作者日期顯示:
response.write("Vert.x is alive!");
response.end();
但這僅顯示一個HTML文本當我連接本地主機:9999與瀏覽器。我一直在這個小時,我不知道我是否做得對。
有人可以幫忙嗎?
編輯:
這裏我做起方法:
public void start() throws Exception {
httpServer = vertx.createHttpServer();
public void start() throws Exception {
httpServer = vertx.createHttpServer();
httpServer.requestHandler(new Handler<HttpServerRequest>() {
@Override
public void handle(HttpServerRequest request) {
System.out.println("incoming request!");
if(request.method() == HttpMethod.POST){
HttpServerResponse response = request.response();
response
.end();
}
}
});
httpServer.listen(9999);
如果您看到此文本,那麼您已收到來自服務器的響應...或者您是否想要執行更具體的操作?另請參閱本文 - [如何使用VERTX處理程序獲取POST表單數據?](http://stackoverflow.com/questions/14390322/how-to-get-post-form-data-using-vertx-handlers) –
Ty爲你的答案, 是的,我想做更具體的事情。在我的html頁面中,我有一個按鈕,當我點擊我繼續發佈請求時,然後我想要服務器回答我,最後我想要回調.done以使用服務器中的數據創建.alert 。 – morpheus21
服務器正在偵聽的端口是什麼?你可以發佈你的'start()'方法嗎?.. –