gpt4 book ai didi

javascript - JSONP — "SyntaxError: missing ; before statement"或 "Cross-Origin Request Blocked"

转载 作者:行者123 更新时间:2023-11-28 17:58:18 24 4
gpt4 key购买 nike

我正在尝试使用 Gumroad API对于 WordPress 插件,并试图了解如何进行任何调用,在我的例子中是 GET。

我在 Stack Overflow 上搜索了很多代码示例,但仍然无法在没有任何错误的情况下运行它。

普通 JS 或 jQuery.ajax 是可以接受的。我的 jQuery 版本是 1.12.4

当我使用 JSNOP 时,我在 firefox webdev 工具中得到响应,但在 JS 中仍然收到错误,因为 API 返回 JSON(如 docs 中所述)。

我尝试了多种代码变体:

代码 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);
}
});

代码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);
}

但是错误总是相同的:

错误:

SyntaxError: missing ; before statement

回应:

{
"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
}]
}

请求的 URL

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

enter image description here

<小时/>

我无法使用 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).

非常感谢您的建议!

最佳答案

创建 1 个额外的 php 即:testing.php 文件以获取 JSON 响应,然后从您的 ajax 请求调用该 php 文件。

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);

JavaScript

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

关于javascript - JSONP — "SyntaxError: missing ; before statement"或 "Cross-Origin Request Blocked",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44071878/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com