简体   繁体   English

如何对私有/自定义URL协议发出jQuery GET / AJAX请求?

[英]How to make a jQuery GET/AJAX request to a private/custom URL protocol?

On both Mac and iOS platforms, it is possible to do two-way interchange with the native runtime through custom URI schemes / a NSURLProtocol . 在Mac和iOS平台上, 可以通过自定义URI方案/ NSURLProtocol与本机运行时进行双向交换 For example.. to request an NSImage from a native Objective-C method, you can register your custom handler (a simple string, here i used " mycustomprotocol ") with Webkit / your WebView NSView , and call it from JS like… 例如..要从本机Objective-C方法请求NSImage ,您可以使用Webkit / WebView NSView注册自定义处理程序(一个简单的字符串,这里我使用“ mycustomprotocol ”),并从JS调用它...

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

I would LIKE to be able to use jQuery to do make requests, as at this point, it is more familiar than 90's-style JS.. but as far as I can find on the web, $.get and $.ajax only do http(s) . 我希望能够使用jQuery来做出请求,因为在这一点上,它比90年代风格的JS更熟悉..但据我在网上找到, $.get$.ajax只做http(s)

Would something like 会是这样的

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

override jquery's URL handling? 覆盖jquery的URL处理? I'm a dumbdumb when it comes to JavaScript, I'm sure this is easily done.. I do believe this is how the entire jQuery mobile framework is implemented (via private URI's).. So, why is there nothing on google or SO about it, huh? 当我谈到JavaScript时,我很愚蠢,我确信这很容易完成。我相信这就是整个jQuery移动框架的实现方式(通过私有URI)。所以,为什么google上没有任何内容关于它,对吧? Can i get some help from my sister friends? 我能从姐姐朋友那里得到一些帮助吗?

The basic ajax and get methods use the browser's regular http requests. 基本的ajax和get方法使用浏览器的常规http请求。 For security reasons, ajax type calls will never work with a custom protocol. 出于安全原因,ajax类型调用永远不会使用自定义协议。 If you try to develop a website that isn't on a server and use .ajax, you'll notice it will do nothing. 如果您尝试开发一个不在服务器上并使用.ajax的网站,您会发现它什么都不做。 You'll have to start from the ground up and make a custom request handler altogether, not just alter something within jQuery. 你必须从头开始并完全自定义请求处理程序,而不仅仅是改变jQuery中的内容。

Here is what I did for a callto: protocol, which works in Firefox 24 and IE 10 (I haven't tested any other browsers): 这是我为callto: protocol做的,它在Firefox 24和IE 10中运行(我没有测试过任何其他浏览器):

First - your protocol must already be registered or your computer won't recognize it. 首先 - 您的协议必须已经注册,否则您的计算机将无法识别它。 Second create a hidden link in your markup with your protocol as the href. 其次,使用您的协议作为href在标记中创建隐藏链接。 When I click on a button ( #calltobutton ), then it sets the value of the hidden link ( #clicker ) and then and then clicks it, causing the browser to perform the action. 当我单击一个按钮( #calltobutton )时,它会设置隐藏链接( #clicker )的值,然后再单击它,导致浏览器执行操作。

Then do this: 然后这样做:

$('#calltobutton').click(function () {
    initCallTo(1234567890);
});


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

Now, the method of clicking on the callto link is a but strange. 现在,点击callto链接的方法很奇怪。 You can read more about that in this here thread . 你可以在这里找到更多相关信息。

This comes a little late, but you can use https://github.com/ded/reqwest to do this: 这有点晚了,但您可以使用https://github.com/ded/reqwest执行此操作:

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

Only way I could get this to work was to do a browser window redirect using the DOM in JS. 只有我可以使用它才能使用JS中的DOM进行浏览器窗口重定向。 Calling from JQuery or even the reqwestJS library mentioned on this page didnt work for me with mobile safari and an app that listens for custom URI schemes. 从JQuery调用甚至本页提到的reqwestJS库对我来说都不适合使用移动safari和一个监听自定义URI方案的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM