gpt4 book ai didi

javascript - 得到 404 错误,不知道为什么

转载 作者:行者123 更新时间:2023-11-30 16:42:13 27 4
gpt4 key购买 nike

您好 我尝试为数据编写一些流式 Web 应用程序,所以我使用了一个教程并开始构建我的 Web 应用程序、我的客户端、我的 JS 和我的 HTML 文档。现在我的 HTML 文档有 2 个输入框,如果我填写两个示例(名称:DAGO,值:2.2),1 个用于名称,1 个用于值,然后我单击 GET 按钮,它将此数据保存到 HashMap 目前有效。

当我尝试使用 GET 按钮读取 DAGO 的值时,它告诉我 404 not found ...但 get 函数在我的 WebApp 中,我不知道为什么会出现此错误。

希望我对我的问题的描述足够好,如果没有请评论,我现在将添加您需要的源代码。

JavaScript 代码

$(document).ready(function(){
$("#get").click(function(){
var companyName = $("#companyname").val();

$.ajax({
url: "http://localhost:8080/TimeStreamingTestartID-1.0-SNAPSHOT/app/simplestockmarket/" + companyName,
type: "GET",
success: function(value){
$("#marketprice").val(value);
}
});
});


$("#set").click(function(){
var companyName = $("#companyname").val();
var marketPrice = $("#marketprice").val();

$.ajax({
url: "http://localhost:8080/TimeStreamingTestartID-1.0-SNAPSHOT/app/simplestockmarket/" + companyName,
type: "PUT",
contentType: "text/plain",
data: marketPrice,
success: function(value){
alert("SUCESS: SET!");
}
})
});
});

Java代码(设置和获取函数)

@Path("/simplestockmarket")
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)

public class SimpleStockMarketResource {

private MarketPriceStore marketPriceStore = new MarketPriceStore();

@PUT
@Path("/{companyname}")
public void store(@PathParam("companyname") String companyName, Double marketPrice){
companyName = companyName.toUpperCase();
marketPriceStore.store(companyName,marketPrice);
}

@GET
@Path("/{companyname}")
public Response retrieve(@PathParam("companyname") String companyName){
companyName = companyName.toUpperCase();
Response.ResponseBuilder responseBuilder =Response.status(Response.Status.NOT_FOUND);
marketPriceStore.retrieve(companyName).ifPresent(
marketPrice -> responseBuilder.status(Response.Status.OK).entity(marketPrice)
);
return responseBuilder.build();
}
}

HTML代码

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
Firmen Name:<br>
<input type="text" name="firmenname" id="companyname"><br>
Aktien Wert:<br>
<input type="text" name="aktienwert" id="marketprice"><br>

<button type= "btn btn-primary" id= "get">get</button>
<button type= "btn btn-primary" id= "set">set</button>


<script src="jquery-1.11.2.min.js"></script>
<script src="bootstrap.min.js"></script>
<script src="script.js"></script>


</body>
</html>

最佳答案

private MarketPriceStore marketPriceStore = new MarketPriceStore();

当对象存在时,marketPriceStore是对象的一个​​变量,但是当store函数调用结束时对象消失,所以marketPriceStore消失。

你可以改变

private MarketPriceStore marketPriceStore = new MarketPriceStore();

private static MarketPriceStore marketPriceStore = new MarketPriceStore();

关于javascript - 得到 404 错误,不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31785450/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com