使用Javascript通过Header发送HTTP请求

时间:2016-03-08 11:27:56

标签: javascript jquery ajax http http-headers

我试图将以下http请求发送到twitter api,以便获得最热门的主题

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
...
<script type="text/javascript">  
        $.ajax({
            url: 'https://api.twitter.com/1.1/trends/place.json',
            headers: {
                'Access-Control-Allow-Origin': '*',
                'Authorization':'bearer AAAAAAAAAAAAAAAA************************X3jdHnTJ5GXDWBj',
                'Content-Type':'application/x-www-form-urlencoded'
            },
            method: 'GET'
        });
</script>

然而,我一直收到以下错误:

Failed to load resource: the server responded with a status of 400 (OK)
XMLHttpRequest cannot load https://api.twitter.com/1.1/trends/place.json. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 400.

我不明白我做错了什么。这是我第一次使用JS设置标题等。我尝试使用https://www.hurl.it/发送请求,并正确返回结果。

0 个答案:

没有答案