简体   繁体   English

LastFM API登录-CORS问题

[英]LastFM API login - CORS Issue

I am trying to use the lastFM API. 我正在尝试使用lastFM API。 I have started with a very basic template where all i wanted to do was connect to the LastFM API and authenticate myself. 我从一个非常基本的模板开始,我想要做的就是连接到LastFM API并对自己进行身份验证。 I have a button on my HTML page - 我的HTML页面上有一个按钮-

<button id="auth">AUTHENTICATE</button>

Here's the jQuery function to handle the click event - 这是处理click事件的jQuery函数-

$(document).ready(function() {
            $("#auth").click(function() {
                console.log("authenticate called");
                var myUrl = "http://www.last.fm/api/auth/?api_key=32*************8a*****2";
                /*$.get(url,function(data) {
                    alert("data");
                });*/
                $.ajax({

                    // The 'type' property sets the HTTP method.
                    // A value of 'PUT' or 'DELETE' will trigger a preflight request.
                    type: 'GET',

                    // The URL to make the request to.
                    url: myUrl,


                    xhrFields: {
                        withCredentials: false
                    },
                    crossdomain : true,

                    headers: {
                        // Set any custom headers here.

                    },

                    success: function(data) {
                        // Here's where you handle a successful response.
                    },

                    error: function(data) {
                        console.log(data);

                });
            });
        });

I am running this on my localhost. 我在本地主机上运行它。 As you can see from the AJAX request, it supports CORS. 从AJAX请求中可以看到,它支持CORS。 I can also see CORS header attributes being added to my request headers. 我还可以看到CORS标头属性已添加到我的请求标头中。 But the server needs to respond with the CORS headers too like Access-Control-Allow-Origin. 但是服务器也需要使用CORS标头进行响应,例如Access-Control-Allow-Origin。 But the response does not contain any such headers. 但是响应中不包含任何此类标头。

But lastFM API supports CORS, so shouldn't it be sending these attributes in the response headers? 但是lastFM API支持CORS,因此它不应该在响应头中发送这些属性吗? Also, now how can I make use of CORS to authenticate my application? 另外,现在如何使用CORS对应用程序进行身份验证?

PS - I know I can use JSONP, but I want to know if there is any way I can handle this using CORS? PS-我知道我可以使用JSONP,但是我想知道是否可以使用CORS处理此问题?

Thanks to @potatopeelings, I got the answer. 感谢@potatopeelings,我得到了答案。 What we need was not to call the authentication url, but to redirect it to a seperate page which will ask me to authorize the application to use the lastFM account. 我们所需的不是调用身份验证URL,而是将其重定向到一个单独的页面,该页面将要求我授权应用程序使用lastFM帐户。 I also provided a callback URL which will redirect to my application after I have given the authorization. 我还提供了一个回调URL,在获得授权后,该URL将重定向到我的应用程序。 So my final URL is like - 所以我的最终到达网址就像-

myURL = "http://www.last.fm/api/auth/?api_key=XXX&cb=http://localhost:63342"

And I removed my AJAX call to do the following - 并且我删除了AJAX调用以执行以下操作-

window.location.replace(myUrl);

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

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