通过Cordova访问需要白名单引用域名的远程API [英] Accessing a remote API that requires a whitelisted referring domain name via Cordova

查看:369
本文介绍了通过Cordova访问需要白名单引用域名的远程API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在走上一条不可能的路之前,我有一个普遍使用的问题.我想构建一个可以与外部网站上的API(读取数据和更新日期)交互的应用程序.外部站点提供了两种连接到API的方法,即一组API调用的客户端和服务器.客户端方法需要将引用域列入白名单,服务器方法需要将引用站点的IP列入白名单.

I have a general use question before I go down a road that might not be possible. I want to build an app that can interface with an API (read data and update date) on an external website. The external site offers two methods to connect to the API, a client and server set of API calls. The client method requires whitelisting the referring domain and the server method requires whitelisting the IP of the referring site.

如果我要建立一个常规网页并想连接到API,那么如果HOST.com是我要连接到的站点,而CLIENT.com是我要从FROM连接的站点,那么我会将CLIENT列入白名单. com在HOST.com的安装文件中.

If I'm building a regular webpage and want to connect to the API then if HOST.com is the site I want to connect TO and CLIENT.com is the site I want to connect FROM, then I would whitelist CLIENT.com in the setup files of HOST.com.

或者,如果我想通过服务器API(例如php)访问HOST.com,则需要在HOST.com的设置文件中将CLIENT.com的网站IP列入白名单.

Alternatively, if I want to access HOST.com via the server API's (like php) then I need to whitelist CLIENT.com's website IP in the setup files of HOST.com.

我的问题是,这可以在Cordova内完成吗?我显然无法将设备的IP列入白名单,但是设备可以将引用域传递到HOST.com,以便它通过白名单吗?

My question is, is this something that can be done within Cordova? I obviousy won't be able to whitelist an IP of the device, but can the device pass the a referring domain to the HOST.com so it passes the whitelist?

或者我是否必须在云中设置某种与该应用程序交互的Web应用程序,然后该托管的应用程序使用白名单的域(甚至通过服务器API和白名单的IP)访问HOST.com.

Or am I going to have to setup some kind of webapp hosted in the cloud that the app interfaces with and that hosted app then accesses HOST.com with a whitelisted domain (or even via Server API and a whitelisted IP).

我意识到这个问题的普遍性,但对任何反馈都将有所帮助.要指出的是,这与Cordova应用程序上的白名单无关,这是远程服务器上的服务器配置.如果未将CLIENT.com列入白名单,则CLIENT.com无法访问HOST.com.

I realize the generality of this question, but would be helpful with any feedback. Want to point out this is NOT about whitelisting on the Cordova app, this is a server configuration on the remote server. If CLIENT.com is not whitelisted then CLIENT.com can NOT access HOST.com Thanks in advance.

推荐答案

您可以通过简单的ajax调用进行任何操作.如果您具有PHP知识,则可以使用它来发送到另一个URL(例如某些数据):

You can make anything with simple ajax call. If you have PHP knowledge you can use this to send to another url(for example some data):

[PHP-用ajax调用]

    $postdata = http_build_query(
        array(
            'name' => $name,
            'surname' => $surname
        )
    );

    $opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => 'Content-type: application/x-www-form-urlencoded',
            'content' => $postdata
        )
    );

    $context  = stream_context_create($opts);
    //call some api
    $result = file_get_contents('http://someurl.com/write.php?key=2q2q3q&name='.$name.'&surname='.$surname.', false, $context);

if($context) {
  echo "success"; // ajax returned
} else {
  echo "error"; // ajax returned
} 

因此,在ajax调用(更新之类)之后,您也可以在其他host.com上发送该数据.

So after ajax call (updating or something), you can send that data also on other host.com.

这篇关于通过Cordova访问需要白名单引用域名的远程API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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