【发布时间】:2018-02-18 11:53:53
【问题描述】:
我正在尝试为脚本编写代码以检查 Udemy 上优惠券代码的有效性。课程 ID 为“1524736”,本案例的优惠券代码为“UDEMYSTUDIO”。
我正在使用 $.getJSON 从站点请求信息,但它无法处理错误:
加载失败 https://www.udemy.com/api-2.0/course-landing-components/1524736/me/?components=redeem_coupon&couponCode=UDEMYSTUDIO: 请求中不存在“Access-Control-Allow-Origin”标头 资源。因此,不允许访问 Origin 'null'。
我该如何解决它,有没有更好的方法来解决它? 提前致谢。
下面是我正在使用的代码。
<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript - read JSON from URL</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
</head>
<body>
<div class="mypanel"></div>
<script>
var url= 'https://www.udemy.com/api-2.0/course-landing-components/1524736/me/?components=redeem_coupon&couponCode=UDEMYSTUDIO'
$.getJSON(url, function(data) {
var text = `Apply_State: ${data.redeem_coupon.is_applied}<br>
Error: ${data.redeem_coupon.error}<br>
Code: ${data.redeem_coupon.code}`
$(".mypanel").html(text);
});
</script>
</body>
</html>
【问题讨论】:
标签: javascript jquery html json web-scraping