制作一个与协议无关的jQuery ajax调用 [英] Making a protocol agnostic jquery ajax call

查看:123
本文介绍了制作一个与协议无关的jQuery ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有嵌入在各个网站的Widget。愿我们的小部件根据我们所嵌入网站的协议(HTTP或HTTPS)进行Ajax调用到我们的服务器。 是否有一个常见的​​做法,这样做呢?

使用协议无关的网址(见下文)不工作:

  $。阿贾克斯({
    网址://cdn.example.com/serviceName,
    ...
});
 

解决方案

也不会转回去这项工作?

  VAR myUrl =(window.location.protocol +'//cdn.example.com/serviceName');

$阿贾克斯({
    网址:myUrl,
    ...
});
 

We have a widget that is embedded in various website. We would like the widget to make an ajax call to our server according to the protocol (http or https) of the website in which we are embedded. Is there a common practice to do that?

Using a protocol agnostic url (see below) doesn't work:

$.ajax({
    url : '//cdn.example.com/serviceName',
    ...
});

解决方案

Wouldn´t this work?

var myUrl = (window.location.protocol + '//cdn.example.com/serviceName');

$.ajax({
    url : myUrl,
    ...
});

这篇关于制作一个与协议无关的jQuery ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆