local-host 2018-08-14 07:03 采纳率: 100%
浏览 65

通过jQuery访问API

I have an example in php how to get to api, how can I do it using jquery? I do not know PHP and I would have to get data from api.

PHP example:

$address = 'https://mywebsite.com/api/?gate=clients/getNewsletter/0/json';

$request = array();
$request['authenticate'] = array();
$request['authenticate']['system_key'] = sha1(date('Ymd').'mypass');
$request['authenticate']['system_login'] = "mylogin";
$request['params'] = array();
$request['params']['shops'] = array();
$request['params']['shops'][0] = array();
$request['params']['shops'][0]['shop_id'] = 0;
$request['params']['shops'][0]['approval'] = 'y';
$request['params']['date'] = array();
$request['params']['date']['from'] = '2018-08-10';
$request['params']['date']['to'] =  '2018-08-13';
$request['params']['return_elements'] = array();
$request['params']['return_elements'][0] = "1";
$request['params']['results_page'] = 0;
$request['params']['results_limit'] = 100;

$request_json = json_encode($request);
$headers = array(
    'Accept: application/json',
    'Content-Type: application/json;charset=UTF-8'
);

$curl = curl_init($address);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
curl_setopt($curl, CURLINFO_HEADER_OUT, 1);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $request_json);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($curl);
$status = curl_getinfo($curl);
curl_close($curl);

Can this be done on a jquery?

  • 写回答

1条回答 默认 最新

  • 撒拉嘿哟木头 2018-08-14 09:28
    关注

    You can use the jQuery ajax function: https://api.jquery.com/jQuery.ajax/

    var d = new Date();
    var username = d.getFullYear() + d.getMonth() + d.getDate() + 'mypass';
    var parameters = {
        'authenticate': {
            'system_key': crypto.subtle.digest('SHA-1', new TextEncoder("utf-8").encode(username)),
            'system_login': 'mylogin'
        },
        'params': {
            'shops': [{'shop_id': 0, 'approval': 'y'}],
            'date': []
        }
    };
    $.ajax({
        url: 'https://mywebsite.com/api/?gate=clients/getNewsletter/0/json',
        type: 'post',
        dataType: 'json',
        contentType: 'application/json',
        data: JSON.stringify(parameters),
        success: function(data) {
          // process data
          console.log(data);
        },
        error: function(a, b, c) {
          // error handling
          console.log(a, b, c);
        }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?