How to make jQuery GET / AJAX request a private / custom url protocol?

On Mac and iOS platforms, you can perform two-way exchanges with your own version through user schemes URI / a NSURLProtocol . For example .. to request NSImage from the Objective-C native method, you can register your own handler (a simple line, here I used " mycustomprotocol ") with Webkit / your WebView NSView and call it from JS like ...

 var theURL = 'mycustomprotocol:///' + (textField.value); img.innerHTML = '<img src="'+theURL+'" id="string"/>'; 

I would like to use jQuery to execute queries, because at the moment it is better known than 90 style JS-style. However, as far as I can find on the Internet, $.get and $.ajax only http(s) .

There will be something like

 javascript:document.location = 'mycustomprotocol://' 

override jquery url handling? I am sure it is easy to do. I believe that the whole jQuery infrastructure for mobile devices is implemented (through private URIs). So why is there nothing on Google or So about it, huh? Can I get help from my sister's friends?

+6
source share
4 answers

The basic ajax and get methods use normal browser HTTP requests. For security reasons, calls like ajax will never work with a custom protocol. If you try to create a website that is not on the server and use .ajax, you will notice that it will not do anything. You will need to start from scratch and generally create your own query processor, and not just change something in jQuery.

+2
source

Here is what I did for the callto: protocol, which works in Firefox 24 and IE 10 (I have not tested other browsers):

First, your protocol must already be registered or your computer will not recognize it. Secondly create a hidden link in your markup with your protocol as href. When I click on the button ( #calltobutton ), it sets the value of the hidden link ( #clicker ), and then clicks on it, forcing the browser to perform the action.

Then do the following:

 $('#calltobutton').click(function () { initCallTo(1234567890); }); function initCallTo(callto) { $('#clicker').attr('href', "callto:" + callto); $('#clicker')[0].click(); } 

Now the callto link click method is, but weird. You can learn more about this in this thread .

+2
source

It's a bit late, but you can use https://github.com/ded/reqwest to do this:

 reqwest('mycustomprotocol://myaction', function(result) { // ... }); 
+1
source

The only way I could get this to work is to redirect the browser window using the DOM in JS. The call from jQuery or even the reqwestJS library mentioned on this page did not work for me with mobile safari and an application that listens for custom URI schemes.

+1
source

Source: https://habr.com/ru/post/913279/


All Articles