javascript - How to stop a "preflight has invalid HTTP status code 500" error? - Stack Overflow

I am trying to connect to the yelp API. Using Vue and Axios. I am unable to connect. I am getting error

I am trying to connect to the yelp API. Using Vue and Axios. I am unable to connect. I am getting error:

XMLHttpRequest cannot load ;limit=50. Response for preflight has invalid HTTP status code 500

Here is my code:

new Vue({
  el: '#app',
  data: {
      businesses: []
  },

  // Logic Methods

      mounted() {
          var app = this;
          var url = ';limit=50';
          var config = {
              headers: {
                  "Cache-Control": "no-cache",
                  'Content-Type': 'application/x-www-form-urlencoded',
                  "Access-Control-Allow-Origin": '*',
                  "Access-Control-Allow-Methods": 'GET, POST, PATCH, PUT, DELETE, OPTIONS',
                  "Access-Control-Allow-Headers": 'Origin, Content-Type, X-Auth-Token, Accept',
                  "Access-Control-Max-Age": "1728000",
                  'Authorization': 'MyAccessCode'
              }
          };

          axios.get(url, config)
            .then(function(response) {
                console.log(response.headers);
                console.log(response);
                app.businesses = response.data
            })
            .catch(function(error) {
                app.businesses = "ERROR"
            })
  }
})

I am assuming this is something to do with CORS but I am unsure how to solve this. I have the CORS Chrome plugin to enable and have all the headers. Can I get any insight on what the problem can be and how to fix it? Is this something on yelp's behalf?

I am trying to connect to the yelp API. Using Vue and Axios. I am unable to connect. I am getting error:

XMLHttpRequest cannot load https://api.yelp./v3/businesses/search?location=sarnia&limit=50. Response for preflight has invalid HTTP status code 500

Here is my code:

new Vue({
  el: '#app',
  data: {
      businesses: []
  },

  // Logic Methods

      mounted() {
          var app = this;
          var url = 'https://api.yelp./v3/businesses/search?location=sarnia&limit=50';
          var config = {
              headers: {
                  "Cache-Control": "no-cache",
                  'Content-Type': 'application/x-www-form-urlencoded',
                  "Access-Control-Allow-Origin": '*',
                  "Access-Control-Allow-Methods": 'GET, POST, PATCH, PUT, DELETE, OPTIONS',
                  "Access-Control-Allow-Headers": 'Origin, Content-Type, X-Auth-Token, Accept',
                  "Access-Control-Max-Age": "1728000",
                  'Authorization': 'MyAccessCode'
              }
          };

          axios.get(url, config)
            .then(function(response) {
                console.log(response.headers);
                console.log(response);
                app.businesses = response.data
            })
            .catch(function(error) {
                app.businesses = "ERROR"
            })
  }
})

I am assuming this is something to do with CORS but I am unsure how to solve this. I have the CORS Chrome plugin to enable and have all the headers. Can I get any insight on what the problem can be and how to fix it? Is this something on yelp's behalf?

Share Improve this question edited Jan 25, 2017 at 7:49 Divins Mathew 3,1964 gold badges24 silver badges34 bronze badges asked Jan 25, 2017 at 7:21 Lewis MenelawsLewis Menelaws 1,1865 gold badges21 silver badges45 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

First things first. The headers you are passing to your request are not meant as request headers:

"Access-Control-Allow-Origin": '*',
"Access-Control-Allow-Methods": 'GET, POST, PATCH, PUT, DELETE, OPTIONS',
"Access-Control-Allow-Headers": 'Origin, Content-Type, X-Auth-Token, Accept',
"Access-Control-Max-Age": "1728000",

As you can see here they should be used by the server in a response to specify CORS rules. So there is no positive effect by putting them in a request - in fact might be even what's causing the issue (although it should be a 4xx error).

Secondly, the preflight request receiving a 500 typically means that the server has some issue dealing with the request you're making and there is nothing you can do to fix it (in theory). In practice however APIs may often give 500 errors when some request you make is wrong, and by changing it you can obtain a success (wrong implementation but often seen in practice).

You should look into more info regarding the error - start simple (e.g. using Postman or a simple HTTP tool) by providing the minimum of info (e.g. POST https://api.yelp./v3/businesses/search?location=sarnia with your Authorization header only). If successful, add more of the stuff you're actually using (params, different content-type, etc) to see when you get an error.

You can test this way the API itself bypassing Vue, axios and the browser which may change the requests you make. This way you can find out the source of the problem. Again, it's most likely an issue with the server, but just might be caused by something specific in your client's requests.

Good luck!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745658067a4638662.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信