简体   繁体   English

JSONP —“ SyntaxError:丢失; 声明前”或“跨域请求被阻止”

[英]JSONP — “SyntaxError: missing ; before statement” or “Cross-Origin Request Blocked”

I'm trying to use Gumroad API for Wordpress plugin, and trying to understand how to make any call, GET in my case. 我正在尝试将Gumroad API用于Wordpress插件,并试图了解如何进行任何调用(以我为例)。

I searched a lot for code examples on Stack Overflow but still can't run it without any errors. 我在Stack Overflow上搜索了很多示例代码,但仍然无法运行而没有任何错误。

Plain JS or jQuery.ajax is acceptable. 可以使用纯JS或jQuery.ajax。 My jQuery version is 1.12.4 我的jQuery版本是1.12.4

When I use JSNOP I get the response in firefox webdev tools, but still get an error in JS, becouse the API returns JSON (as described in docs ). 当我使用JSNOP时,我在firefox webdev工具中得到了响应,但是在JS中仍然出现了错误,因为API返回了JSON(如docs中所述 )。

I tried numbers of code variations: 我尝试了多种代码变体:

Code 1 代码1

jQuery.ajax({
    dataType: 'jsonp',
    url: 'https://api.gumroad.com/v2/products',
    data: {
        'access_token': '676234257caeb63ca7683c39d14e0091387a1a36af0c2135f989d0fd84ffc0c5'
    },
    success: function(data){
        console.log(data);
    },
    error: function(d){
        console.log(d);
    }
});

Code 2 代码2

jQuery(document).ready(function() { 
    var url = 'https://api.gumroad.com/v2/products/';
    url += '?method=getQuote';
    url += '&format=jsonp';
    url += '&lang=en&';
    url += 'jsonp=myJsonMethod';
    url += '&?callback=?';
    url += '&access_token=676234257caeb63ca7683c39d14e0091387a1a36af0c2135f989d0fd84ffc0c5';
    jQuery.getJSON(url);
});

function myJsonMethod(response){
  console.log (response);
}

But the error is always the same: 但是错误总是相同的:

Error: 错误:

SyntaxError: missing ; before statement

Response: 响应:

{
  "success": true,
    "products": [{
      "name": "Test",
      "url": "https://s3.amazonaws.com/gumroad/attachments/1295292066926/b81638b60726496a98e63d2cc7d80075/original/IMG_09032017_123025.png",
      "preview_url": "https://static-2.gumroad.com/res/gumroad/1295292066926/asset_previews/a4a204f68be7087dd360a142e620728e/retina/Color_Wheel.png",
      "description": "\n\u003cp\u003eOffer test\n\n\u003c/p\u003e\n",
      "customizable_price": false,
      "webhook": null,
      "require_shipping": false,
      "custom_receipt": "",
      "custom_permalink": null,
      "subscription_duration": null,
      "id": "MmaHg-V0-uqWW4T2W_-LLw==",
      "custom_product_type": null,
      "countries_available": [],
      "price": 1000,
      "currency": "usd",
      "short_url": "https://gum.co/qiHIX",
      "formatted_price": "$10",
      "published": true,
      "shown_on_profile": true,
      "file_info": {
        "Size": "187 KB",
        "Resolution": "1080p"
      },
      "max_purchase_count": null,
      "deleted": false,
      "custom_fields": [],
      "custom_summary": "",
      "variants": [],
      "sales_count": 0,
      "sales_usd_cents": 0,
      "view_count": 6
    }]
}

Requested URL 要求的网址

https://api.gumroad.com/v2/products?callback=jQuery112409655243732650752_1495261525390&access_token=676234257caeb63ca7683c39d14e0091387a1a36af0c2135f989d0fd84ffc0c5&_=1495261525391

在此处输入图片说明


I can't use JSON request type becouse it couse of another error: 我无法使用JSON请求类型,因为它会导致另一个错误:

Cross-Origin Request Blocked:
    The Same Origin Policy disallows reading the remote resource
    at https://api.gumroad.com/v2/products.
    (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

Thanks a lot for any suggestions! 非常感谢您的任何建议!

Create 1 additional php ie : testing.php file to get that JSON response , then call that php file from your ajax request. 创建1个其他php,即: testing.php文件以获取JSON响应,然后从ajax请求中调用该php文件。

PHP FILE PHP文件

$url = 'https://api.gumroad.com/v2/products?callback=jQuery112409655243732650752_1495261525390&access_token=676234257caeb63ca7683c39d14e0091387a1a36af0c2135f989d0fd84ffc0c5&_=1495261525391';
header('Content-Type: application/json');
echo file_get_contents($url); 

Javasript Javasript

jQuery.ajax({
    url  : 'testing.php',
    method: 'GET',
    success: function( data, txtStatus) {
    console.log(data);
    }       
});

The API call will only work via the server. API调用只能通过服务器进行。 Use PHP / cURL. 使用PHP / cURL。

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => "https://api.gumroad.com/v2/products",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_HTTPHEADER => array(
     "authorization: XX Your Profile Key",
     "cache-control: no-cache"
    ),
));

//Fetch value from Gumroad
$response = curl_exec($curl);

//Get products from string
$prod = filter_var($response, FILTER_SANITIZE_NUMBER_INT);

//Throw error
$err = curl_error($curl);
curl_close($curl);

//Print result
if ($err) {
    echo "400";
} 
else {
    echo $prod;
}

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

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