0

在调用我编写的 java REST 服务器时,我遇到了一些 ajax 困难(并且对 ajax 很陌生,但已经进行了相当多的搜索)。我在 Rest 服务器中运行了一个测试 html 页面(使用 Jetty),我使用了以下调用——使用 .get 可以成功运行:

        $('#getRVLakeForm').submit(function(e) {
            var handle = $('#insertRVLakeHandle').val();
            var lakekey = $('#RVLakeKey').val();
            var temp = $('#RVLakeTemp').val();
            var speed = $('#RVLakeWindspeed').val();
            var degrees = $('#RVLakeWindDegrees').val();;
            $.get('${pageContext.request.contextPath}/api/recent/lake/' + handle + "/" + temp + "/" +  speed + "/" + degrees + "/" + lakekey, function(data) {
                alert(data.lake[0].oid);

                $('#RMLakeResponse').text("back");
            });

这是 Rest 服务器返回的 JSON:

{"user":[],"lake":[{"oid":"519b9a1a3004f8fa1287502a","type":"Lake","handle":"nightstalker","viewedhandle":"","viewedlaketag":" TXFORK","viewedusername":"","timestamp":1369152026668}]}

此调用执行 Rest Api 并按预期返回 JSON ...当我尝试从 HTML/PHP 应用程序调用相同的 Rest Api 时,在 MAMP 下运行——ajax 调用适用于出站调用——我可以调试Java Rest 服务器并看到调用进来,它按设计执行,创建 JSON 发送出去。问题是我从来没有在 ajax 调用中调用成功函数(我也没有看到调用的错误函数)。

    urlrecent = "http://localhost:8080/server/api/recent/lake/" + handle + "/" + temp + "/" +  speed + "/" + degrees + "/" + lakekey;

    $.ajax({
        type: "GET",
        contentType: "application/json",
        dataType: "jsonp",
        url: urlrecent,
        success: function (data) {
            alert("hello there!");
            alert(data)
        },
        error: function () {
            alert('failed');
        }
    });

我什至尝试了一个 getJSON 代替..没有运气。同时,我将继续在 Stack 和网络上搜索可行的东西。我也尝试过只有“json”的数据类型。

提前致谢。

=====

是的,修改了 contentType,但这没关系。

4

1 回答 1

0

在 IE8 中尝试并设置dataType: "html"

urlrecent = "http://localhost:8080/server/api/recent/lake/" + handle + "/" + temp + "/" +  speed + "/" + degrees + "/" + lakekey;

    $.ajax({
        type: "GET",
        contentType: "application/json",
        dataType: "html",
        url: urlrecent,
        success: function (data) {
            alert("hello there!");
            data = JSON.parse(data);
            alert(data)
        },
        error: function () {
            alert('failed');
        }
    });

如果您data = JSON.parse(data)在 alert 中收到语法错误或“hello there”,这是编码问题。在这种情况下,您应该使用response.setCharacterEncoding("UTF8")

于 2013-05-22T18:20:30.073 回答