I was wondering what the best way to send data from an HTTPS webpage loaded in a browser to a server running on localhost would be. Currently I have a solution that works with both HTTP and HTTPS pages, but with HTTPS pages the browser will output a Mixed Content
warning (as it should). This can be bypassed by changing browser settings, but I'd rather not have users do that. This is my javascript that is loaded in the browser through a bookmark (not really sure if this is the best way to do it, but its browser independent):
function sendLocalPOST(player, song) {
var url = "http://localhost:13337/";
$.ajax({
type: "POST",
crossdomain: true,
contentType: "application/json; charset=utf-8",
url: url,
dataType: "json",
data: { 'player': player, 'song': song },
success: function (data) {
}
});
}
and here are some important snippets from the C# server code:
public WebAppHandler()
{
// Other non-important stuff
listener = new HttpListener();
listener.Prefixes.Add("http://localhost:13337/");
}
public void pollForSongChanges()
{
try
{
listener.Start();
}
catch (HttpListenerException)
{
return;
}
while (listener.IsListening)
{
var context = listener.GetContext();
ProcessRequest(context);
}
listener.Close();
}
public void start()
{
pr = new Thread(new ThreadStart(pollForSongChanges));
pr.Start();
}
There's another question that I saw on stackoverflow that has a great answer (the accepted one), that requires you to bind an SSL certificate to your application, but does that mean I have to get an actual trusted SSL certificate for a localhost server in order to have it work out-of-the-box on other computers?
Maybe I'm going about this all wrong, just wondering if there is a better way. Thanks for any answers.
@AlexeiLevenkov, do you mean something like this:
function postToIframe(data) {
var url = "http://localhost:13337/";
var target = "npiframe";
$('body').append('<form action="' + url + '" method="post" target="' + target + '" id="postToIframe"></form>');
$.each(data, function (n, v) {
$('#postToIframe').append('<input type="hidden" name="' + n + '" value="' + v + '" />');
});
$('#postToIframe').submit().remove();
}