简体   繁体   中英

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. 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:

        $('#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:

{"user":[],"lake":[{"oid":"519b9a1a3004f8fa1287502a","type":"Lake","handle":"nightstalker","viewedhandle":"","viewedlaketag":"TXFORK","viewedusername":"","timestamp":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. 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).

    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. In the mean time I will continue search Stack and the web for something that will work. I have tried a dataType of just "json" as well.

Thanks in advance.

=====

Yes, munged the contentType, but that didn't matter.

Try it in IE8 and set 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. In this case you should use response.setCharacterEncoding("UTF8")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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