I
I
Ilkhomjon Matazimov2020-03-06 12:17:17
JavaScript
Ilkhomjon Matazimov, 2020-03-06 12:17:17

VK blocks fetch 'GET' request. How to decide?

I need to get json response from vkontakte. There is a special API.

Here is the link: ' https://api.vk.com/method/friends.getSuggestions?f... '+$filter+'&fields='+$fields+'&count=500&access_token='+$token+'&v=5.103'

But using the fetch and xhr methods, VKontakte blocks the request with the following response: "Access to XMLHttpRequest at ' https://api.vk.com/method/friends.getSuggestions?f... ' from origin ' vk ' has been blocked by CORS policy : No 'Access-Control-Allow-Origin' header is present on the requested resource."

How to decide? In php it is solved very simply, with the following request: json_decode(file_get_contents(' https://api.vk.com/method/friends.getSuggestions?f...'.$filter.'&fields='.$fields.'&count=500&access_token='.$token.'&v=5.103'));

Here is the code with an error in JavaScript: 5e62146167be4705711959.png

And here is with php, where there is no error:5e6214e8d490d545565785.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilhomjon Matazimov, 2020-03-06
@mr_qpdb

Thanks everyone, issue resolved. Solved the issue with jquery.
With this code:
$.ajax({
url: ' https://api.vk.com/method/friends.getSuggestions?f... '+$filter+'&fields='+$fields+'&count=500&access_token='+ $token+'&v=5.103',
type: 'GET',
dataType: 'jsonp',
crossDomain: true,
success: function(data){
console.log(data.response);
}
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question