Http post with hype buttons

I’m trying to post to a web server on a local network using a javascript call.

Each button on my page has a javascript like this:

				$.post("http://" + window.control_ip + ":8080/loop-mode", "data", null, "text")

When I press the button I get this error:
XMLHttpRequest cannot load http://10.10.154.104:8080/loop-mode. Origin http://10.10.154.104 is not allowed by Access-Control-Allow-Origin.

I’m not sure why it is formatting as an XML request. When I use a plain HTML call like the one below it works perfectly. Any idea how I could use a script like the one below in Hype?

 <a onClick="$.post('http://10.10.154.104:8080/loop_mode');return false;" href="">power-on</a><br />

If you are on the same IP, you do not need to call HTTP, just call localhost/loop-mode.

As for the XML request there must be something thats requesting that format, perhaps if you are bringing over html, you should confirm that in the call, rather than text.

Personally, if you are doing a number of .posts, you are best creating a function to handle them, rather than using many with the correct format, more so for debugging:

request = function(url, data, type) {
    $.ajax({
      type: "POST",
      url: "localhost/" + url,
      data: data,
      dataType: dataType
      success: functioin(res) {
           return res;
      }
    });
}

I would add this to your onAppLoad function in Hype, or create one on scene load.

1 Like

Hi Andrew,

I wasn’t on the same domain (one controller was on port 80 and the other on 8080. Moving them both to the same server and addressing the function as you suggest fixed the issue.

Thanks!

Could you post an example of this? I’m trying to get a trigger to function using POST and I’m not sure where to place the code above.

Thanks :slight_smile:

It depends on when you want to make a post call, but the basic flow would be to add an on click handler that runs javascript, and then paste the code there. This example also uses jquery, so you’ll either need to import that library to the Resources Library in the .hype document, or link to a CDNed version in the head HTML. Feel free to elaborate on what you’re trying to do (in another topic if it isn’t too related to this one).