gpt4 book ai didi

JQuery 将 JSON 对象发布到服务器

转载 作者:IT老高 更新时间:2023-10-28 12:47:13 25 4
gpt4 key购买 nike

我创建了一个需要在 jersey 中发布的 json,由 grizzly 运行的具有 REST web 服务的服务器获取需要输出的传入 json 对象。我正在尝试,但不确定如何正确实现。

import java.io.IOException;
import java.io.InputStream;

import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

import org.apache.commons.io.IOUtils;

import javax.ws.rs.*;

@Path("/helloworld")
public class GetData {
@GET
@Consumes("application/json")
public String getResource() {

JSONObject obj = new JSONObject();
String result = obj.getString("name");

return result;
}

}

我有一个在加载时运行此方法的 html 文件

    function sendData() {
$.ajax({
url: '/helloworld',
type: 'POST',
contentType: 'application/json',
data: {
name:"Bob",


},
dataType: 'json'
});
alert("json posted!");
};

最佳答案

要将json发送到服务器,首先要创建json

function sendData() {
$.ajax({
url: '/helloworld',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
name:"Bob",
...
}),
dataType: 'json'
});
}

这是构建 ajax 请求以将 json 作为 post var 发送的方式。

function sendData() {
$.ajax({
url: '/helloworld',
type: 'POST',
data: { json: JSON.stringify({
name:"Bob",
...
})},
dataType: 'json'
});
}

json 现在将位于 json 后置变量中。

关于JQuery 将 JSON 对象发布到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10110805/

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