【问题标题】:Ajax JQuery Send POST data to external apiAjax JQuery 将 POST 数据发送到外部 api
【发布时间】:2017-02-13 09:26:07
【问题描述】:

我到处搜索以获得我的问题的答案。我真的需要一位专家来帮助我解决我的问题。我已经创建了使用 ajax 将数据发布到外部 api url 的代码。

我创建的代码如下:

$.ajax({

  url: "https://www.billplz.com/api/v3/collections",
  beforeSend: function(xhr) {
    xhr.setRequestHeader("Authorization", "Basic " + "73eb57f0-7d4e-42b9-a544-aeac6e4b0f81:");
  },
  type: "POST",
  data: {
    "title": "My First API Collection"
  },
  contentType: 'application/json',
  dataType: 'jsonp',
  success: function(data) {
    alert("Successfully Registered..");
  },
  error: function(xhRequest, ErrorText, thrownError) {
    alert("Failed to process correctly, please try again");
    console.log(xhRequest);
  }

});

我试图从 API 文档中获取这个示例 curl 代码:

# Creates an open collection
curl https://www.billplz.com/api/v3/open_collections \
  -u 73eb57f0-7d4e-42b9-a544-aeac6e4b0f81: \
  -d title="My First API Open Collection" \
  -d description="Maecenas eu placerat ante. Fusce ut neque justo, et aliquet enim. In hac habitasse platea dictumst." \
  -d amount=299

API 文档是here

我已经尝试了上一个问题给出的所有方法,但没有运气。我也尝试在没有 PHP 的 JQuery/AJAX 中执行此操作。

【问题讨论】:

  • 你的服务器地址是什么? httphttps?
  • 使用 https @AtaurRahmanMunna 的外部 api
  • 浏览器控制台显示的错误是什么?你能检查一下吗?
  • 它说“对象无法获取属性。对象可能不再存在。”但对象仍然存在。 @AtaurRahmanMunna
  • 阅读答案并告诉我您的标准是什么。 stackoverflow.com/questions/9310112/…

标签: javascript jquery ajax curl


【解决方案1】:

我不知道我是怎么做到的。但这是我的问题的答案。我希望它可以帮助其他人。

第一件事是不要使用 AJAX 发布您的授权密钥,因为 AJAX 将发布任何人都可以读取的 JSON 对象。对于 curl 过程,我们需要使用服务器端脚本,如 Perl、PHP、Python、Ruby、JavaScript (Node)、Scala、Java、Go、ASP.NET 或 ColdFusion。

在我的例子中,我使用 PHP 来做 curl 过程。以下是我的 ajax 帖子代码:

$.ajax({
            url: 'creating_bill.php',
            data: { 
                item : 'item'
            },
            type: "POST",
            dataType: "json",
            success: function (data) {
               alert('Success ! You will redirect in 100 seconds');
               console.log(data)               
               window.open(data.url, '_blank');
                 setTimeout(function()
                 {
                    window.location = 'index.html';
                 },10000);
            },
            async: false,
            error: function(data) {
                handleRequestError(data);
            }
        })   
}

下面是我在 php 中做 curl 过程的代码:

<?php

$item = $_POST['item'];


$api_key = '';
$api_url = '';
$collection_id = '';
 
$data = array(
          'item' => $item,
);
 
$process = curl_init($api_url);
curl_setopt($process, CURLOPT_HEADER, 0);
curl_setopt($process, CURLOPT_USERPWD, $api_key . ":");
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($process, CURLOPT_POSTFIELDS, http_build_query($data) );
 
$result = curl_exec($process);
curl_close($process);


print_r($result);
$return = json_decode($result, true);
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    • 2017-04-23
    • 2018-09-14
    • 1970-01-01
    • 2011-08-01
    • 2018-05-04
    • 1970-01-01
    相关资源
    最近更新 更多