简体   繁体   English

Ajax调用Rest服务未成功调用Ajax中的成功函数

[英]Ajax call to Rest service not successfully calling success function in ajax

I am having some ajax difficulties (and am newish to ajax, but have done a fair bit of searching) when calling a java rest server that I have written. 调用我编写的Java Rest服务器时,我遇到了一些Ajax困难(并且是Ajax的新手,但是做了很多的搜索)。 I have a test html page running within the Rest server (using Jetty) and I use the following call -- which works successfully using a .get: 我有一个在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");
            });

Here is the JSON the Rest server returns: 这是Rest服务器返回的JSON:

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

This call executes the Rest Api and gets JSON back as expected... When I attempt to call the same Rest Api from HTML/PHP application, running under MAMP -- the ajax call works on the out bound call -- I can debug the Java Rest server and see the call come in, and it executes as designed creating JSON to send out. 此调用将执行Rest Api并按预期方式返回JSON ...当我尝试从HTML / PHP应用程序调用相同的Rest Api时,在MAMP下运行-ajax调用可用于出站调用-我可以调试Java Rest服务器并看到调用进入,它按照设计执行,创建JSON以发送出去。 The problem is that I never get a call to the success function in the ajax call (I am not seeing the error function called either). 问题是我从来没有在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');
        }
    });

I have even tried a getJSON instead..no luck. 我什至尝试了getJSON代替。 In the mean time I will continue search Stack and the web for something that will work. 同时,我将继续在Stack和网络上搜索有效的内容。 I have tried a dataType of just "json" as well. 我也尝试了仅“ json”的数据类型。

Thanks in advance. 提前致谢。

===== =====

Yes, munged the contentType, but that didn't matter. 是的,私语了contentType,但这没关系。

Try it in IE8 and set dataType: "html" 在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');
        }
    });

if you get syntex error at data = JSON.parse(data) or "hello there" in alert , it is encoding problem. 如果您在alert中的data = JSON.parse(data)或“ hello there”处出现syntex错误,则为编码问题。 In this case you should use response.setCharacterEncoding("UTF8") 在这种情况下,您应该使用response.setCharacterEncoding("UTF8")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM