通过 POST 方法发送和接收 JSON class (jQuery)

Send and receive a JSON class throught a POST method (jQuery)

我正在尝试使用 class 之类的输入调用 POST 方法,并从服务器获取结果集合,就像我对 junit 测试所做的那样,您可以在下面看到:

@Test
public void test_m11() {

    Client client = ClientBuilder.newClient();

    MyInput i = new MyInput();
    i.setNumero(33);
    i.setTesto("OK");

    Collection<MyPojo> a = client.target(testURL + "m11")
            .request(MediaType.APPLICATION_JSON_TYPE)
            .post(Entity.json(i), Collection.class);
            //.get(Collection.class);

    System.out.println("test_m11 " + a.size());

    assertEquals(2, a.size());
}

但对我来说似乎不可能! 我尝试使用此脚本但没有任何成功结果:

    var myi = {
        testo: "yeah",
        numero: "33"
    };

    // jquery-1.12.0.js & json2.js
    $.post("http://localhost:8080/WebServices/rest/test/m11",

        JSON.stringify(myi),
        function(data){

            $.each(data, function(key, value) {

                $('#r11').append(key + " " + value);
            });
        }, "json");

我在做什么有什么问题吗?你能给我一些建议吗?

你知道有什么网站可以让我更深入地了解有关 class 由 JS (POST) 发送/接收的示例吗?

谢谢!!

尝试使用您正在尝试做的事情的手写版本。

    var myi = {
        testo: "yeah",
        numero: "33"
    };

    $.ajax({
        url: "http://localhost:8080/WebServices/rest/test/m11",
        type: 'POST',
        contentType: 'application/json',
        data: JSON.stringify(myi)
    })
        .done(function () {
            // Handle good response
        })
        .fail(function (err) {
            // Handle bad response
        });