How to have NanoHTTPD respond to AJAX

Ian

I'm attempting to get NanoHTTPD (on an android device) to respond to AJAX requests in a way that the requesting javascript can interpret the response.

I've implemented the NanoHTTPD serve method:

    public NanoHTTPD.Response serve(String uri, NanoHTTPD.Method method,
                          Map<String, String> header,
                          Map<String, String> parameters,
                          Map<String, String> files) {

        String msg = "Hello, you have connected.";
        return newFixedLengthResponse( msg );
    }

And if I connect from the local webbrowser to "http://127.0.0.1:8080" it loads a page with the source:

<html>
    <head></head>
    <body>Hello, you have connected.</body>
</html>

So far so good, although I'm not sure where the html formatting is introduced.

But what I am stuck on is if I use AJAX from javascript to try to pass data:

$.ajax({
        url: 'http://127.0.0.1:8080',
        type: 'POST',
        data:{ printData: dataToPrint },
        success: function(d){
            alert('success');
        },
        error: function (jqXHR, textStatus) {
            alert("failed, jqXHR: " + jqXHR.responseText + "  " + jQuery.parseJSON(jqXHR.responseText) + "  textStatus: " + textStatus);
        }
    })

(This is just one example, I've tried success/fail/done/error methods, I've tried specifying the datatype, I've tried different parameters in the return functions, none of it works). When this javascript is run the NanoHTTPD server receives the printData just fine, but when it sends it response it is only ever the error/fail method that is triggered and the method parameters never contain anything - I cannot set the status or the return message or anything.

I've tried different returns from the Serve method including:

String mime_type = NanoHTTPD.MIME_PLAINTEXT;

String msg = "{\"status\":\"1\",\"responseText\":\"this is the response\"}";

InputStream testReply = new ByteArrayInputStream(msg.getBytes(StandardCharsets.UTF_8));

// return newFixedLengthResponse(NanoHTTPD.Response.Status.OK, "", msg);
// return new NanoHTTPD.Response( NanoHTTPD.Response.Status.OK, mime_type, testReply);
// return NanoHTTPD.newFixedLengthResponse( NanoHTTPD.Response.Status.OK, mime_type, msg);
// return NanoHTTPD.newFixedLengthResponse(msg);

None of these work.

I also tried this javascript:

$.get("http://127.0.0.1:8080", function( my_var ) {
        console.log(my_var);
    });

If this is run my breakpoint on NanoHTTPD is triggered, but the javascript method is not triggered at all.

sohan nohemy

I think you need to add these headers in your server response:

    Access-Control-Allow-Origin: *
    Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE
    Access-Control-Max-Age: 86400

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to have NanoHTTPD respond to AJAX

From Dev

How to respond to ajax call in Rails

From Dev

How to respond to ajax calls on the server side?

From Dev

how to delay ajax respond for number of seconds

From Dev

How to respond to ajax call from iframe?

From Dev

How to respond to ajax calls on the server side?

From Dev

How to respond correctly to an AJAX call in PHP?

From Dev

How to have interior form respond to key down events?

From Dev

How do I have my Bot respond with arguments?

From Dev

Stacked UI elements: how to have only the visible ones to respond to click

From Dev

How to respond with a status code 500 to an ajax jsonp request

From Dev

ajax respond in codeigniter

From Dev

ajax respond in codeigniter

From Dev

NanoHTTPD how to present variable from app

From Dev

NanoHTTPD How to save uploaded file to sdcard folder

From Dev

How to add File path in NanoHTTPD response

From Dev

How to send an image to Chromecast using NanoHTTPD

From Dev

How to host nanohttpd java app on AWS?

From Dev

Have argparse collect, but not respond to, flags

From Dev

Respond to AJAX with Django template rendered

From Dev

JSON object on ajax respond not available

From Dev

Respond to ajax request with a string in Rails

From Dev

Respond to AJAX with Django template rendered

From Dev

Respond to AJAX request at a later time

From Dev

Using jQuery, how to have click event handler respond for selected table columns?

From Dev

Redux: How can I have two reducers respond to the same action in a certain order?

From Dev

Using jQuery, how to have click event handler respond for selected table columns?

From Dev

How to respond to an XSS attack

From Dev

How to respond to an XSS attack

Related Related

HotTag

Archive