0

I have an Apache2 web server as well as a Python hug server running. Both run on different ports. I now want to sent a request to the hug server using jQuery. As this is cross domain, I think I have to use something similar to this: How do I send an AJAX request on a different port with jQuery? jsonp with callback parameter.

My questions is: - is my approach reasonable? - does hug support jsonp with callback? - could there be a better solution to communicate between javascript and the Python hug API?

zugabe
  • 31
  • 3

1 Answers1

0

If you just need to send a GET request on your API you can use the jQuery method:

$.getJSON(your_url, function(json_received){
      console.log(json_received);
    });

If you need to send data trough a POST request you need to add more parameters in ajax method :

    $.ajax({
          type: 'POST',
          contentType: 'application/json; charset=utf-8',
          url: your_url,
          dataType: 'json',
          data: JSON.stringify({'message':'your_message}),           
          success: function(data_received) {
            console.log(data_received);
          },
          error: function(error) {
            console.log(error);
          }
      });

In the API response you need to add the parameter {'Access-Control-Allow-Origin': '*'} to allow cross domain.

F Blanchet
  • 1,430
  • 3
  • 21
  • 32