When implementing the Parse Client Key in an Angular application, it may lead to encountering

I have been working on retrieving a class from Parse using a client key with the GET method. Successfully sent a request through Advanced Rest Client for Google Chrome by including X-Parse-Application-Id and X-Parse-Client-Key headers.

[edit] [edit2] Response headers (captured from Chrome Developer Tools OPTIONS):

HTTP/1.1 200 OK

Access-Control-Allow-Headers: X-Parse-REST-API-Key, X-Parse-Javascript-Key, X-Parse-Application-Id, X-Parse-Client-Version, X-Parse-Session-Token, X-Requested-With, X-Parse-Revocable-Session, Content-Type

Access-Control-Allow-Methods: OPTIONS, POST, GET, PUT, DELETE

Access-Control-Allow-Origin: *

Access-Control-Max-Age: 86400

Content-Type: application/json; charset=utf-8

Date: Sun, 29 Nov 2015 04:23:08 GMT

Server: nginx/1.6.0

X-Parse-Platform: G1

X-Runtime: 0.000118

Content-Length: 0

Connection: keep-alive

However, when trying to do the same in an Angular app, I encountered the following error:

XMLHttpRequest cannot load . Request header field X-Parse-Client-Key is not allowed by Access-Control-Allow-Headers in preflight response.

Parse mentions support for using cross-origin resource sharing, and as I had successfully made the request earlier with a different client, I believe the server is not the problem. Modifying the response header wasn't an option regardless.

Below is the code used to create the GET request:

var ng_portal = angular.module("ngPortal", []);
ng_portal.controller("GenResourcesCtrl", ["$http", function($http) {
  $http({
    method: "GET",
    url: PARSE_URL + "/1/classes/GenResources",
    headers: {
      "Content-Type": "application/json",
      "X-Parse-Application-Id": PARSE_APP_ID,
      "X-Parse-Client-Key": PARSE_CLIENT_KEY
    }
  }).then(
    function success(res) {
      console.log(res);
    },
    function error(res) {
      console.log(res);
    }
  );
}]);

Answer №1

When you set custom headers in your request, it will trigger an initial pre-flight (OPTIONS) request. The response to this request must include a specific header called "access-control-allow-headers" with a list of the headers that you are attempting to set.

For more information on CORS and pre-flight requests, please refer to https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

I recommend using browser developer tools to inspect the headers of both the requests and responses to ensure they adhere to the CORS specifications. Based on the error message you've shared, it seems like the server handling the cross-domain call does not support custom headers. If you find otherwise, please update your question with the relevant headers, and I can offer further assistance.

Answer №2

It appears that there is an ongoing issue with Parse.com. After spending an entire hour feeling frustrated, I stumbled upon a helpful post on Google Groups

Here is a relevant quote from the post:

Based on my testing, it seems that neither the client key nor the JavaScript key work properly through JavaScript rest interactions in the browser.

I went ahead and reported this as a Parse Bug:

https://developers.facebook.com/bugs/488204124680438

I believe that these keys should function correctly when used in browser requests without needing to rely on an SDK for support.

I recommend checking out my bug report. I still maintain that enabling these keys to function seamlessly with browser requests is the correct approach since they already work outside of the browser environment.

However, it seems like the issue is not being acknowledged or understood by Parse, as they continue to disable it only in the browser, despite it working fine on other platforms. It just doesn't add up.

In my case, I opted to utilize my JavaScript Key X-Parse-Javascript-Key (even though it is intended for use with their JavaScript SDK according to current documentation) as a successful substitute for X-Parse-Client-Key

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How come I'm not receiving an error message when I enter an incorrect email or password?

Whenever I try to log in with the wrong password and email, no error message is displayed. Instead, it simply logs me in and takes me to the main page. Can someone please assist me in implementing an error message display for incorrect login details? imp ...

What is the best method for freezing an HTML element in relation to a container using either CSS or JavaScript?

I've been diligently researching this topic up until the final day, but unfortunately, I have yet to find a solution. I am in dire need of assistance with my project. Could someone please help me out? One of my related searches led me to Can I positi ...

Show only the items in bootstrap-vue b-table when a filter is actively applied

How can I set my bootstrap-vue b-table to only show items when a filter is applied by the user (i.e., entered a value into the input)? For example, if "filteredItems" doesn't exist, then display nothing? This is primarily to prevent rendering all rows ...

Is it necessary for me to indicate that I need the password from results[0] when the MySQL query only returns one result?

When querying the database and receiving only one row/result, make sure to reference the password column correctly. Use this code to ensure you are getting the correct value: var hash = results[0].password; If you try to use var hash = results.password; ...

Tips for incorporating a multimedia HTML/JavaScript application within C++ programming

I possess the source code for a JavaScript/HTML5 application that operates on the client-side and manages the transmission/reception of audio and video data streams to/from a server. My objective is to develop a C++ application that fully integrates the c ...

What is the best way to import the three.js OBJLoader library into a Nuxt.js project without encountering the error message "Cannot use import statement outside a module

Beginner here, seeking assistance. Operating System: Windows 10. Browser: Chrome. Framework: Nuxt with default configurations I have successfully installed three.js using npm (via gitbash) by running npm install three --save. It is included in the packag ...

Unable to access external API through Jquery due to CORS error when making multipart formposts

There exists an external REST API used for uploading resources to a repository, which can be accessed via the curl command: curl -k -X POST -u username:password \ -F package_id="1234" \ -F file=@"path_to_a_file" \ https:/ ...

A guide on including a class to a DOM element in Angular 6 without relying on Jquery

Currently, I have created a component template using Bootstrap that looks like this: <div class="container"> <div class="row my-4"> <div class="col-md-12 d-flex justify-content-center"> <h2> ...

Exploring the capabilities of Serenity/Js for conducting tests with the protractor-Jasmine framework. Are there any specific features in Serenity that cater to

I am in the process of integrating serenity reports into my tests built using the protractor-jasmine framework. So far, all my research has only shown examples of serenity integration with the protractor-cucumber framework. Below is a sample config file ...

Creating operations in Angular using the Model View Controller (MVC)

What is the procedure for performing an Add operation in MVC using Angular? var addProductModule = angular.module("addProductModule", []); addProductModule.factory("addProductService", ['$http', function ($http) { return { function savePro ...

Ways to achieve 8 columns in a single row using Javascript and Bootstrap

Recently, I created a simple function for searching movies and manipulating them in the DOM. The issue arises when a movie name is entered and the API response returns around 20-30 recommendations. I wanted to display this fetched data in 8 columns per row ...

JavaScript function to evaluate true conditions

I am encountering an issue while calling sub functions connected to if statements within my main function. Even though the sub functions are supposed to return true or false, the expected alerts are not appearing. if (functionAAA()){ alert("111 ...

Choose the row based on the values in the columns

I am facing a situation where I have to choose a specific row in a datatable that displays rows in groups of 10, based on the value of its column. Previously, I successfully used the following code to select the first row. myGroupTable.row(':eq(0)&ap ...

NPM: The registry cannot be found

npm http GET https://registry.npmjs.org/n npm ERR! Error: failed to fetch from registry: n npm ERR! at /usr/share/npm/lib/utils/npm-registry-client/get.js:139:12 npm ERR! at cb (/usr/share/npm/lib/utils/npm-registry-client/request.js:31:9) npm ERR ...

Javascript function encountering difficulty accessing the 'this' variable

I am embarking on a new HTML5 game project, my very first one. Right now, I am working on creating a function that holds variables. While I cannot recall its exact name, I remember the process from a tutorial. In my attempt to set up some of these variable ...

Run JavaScript code with Selenium WebDriver prior to calling get() in Java

Currently, I am attempting to use Selenium in Java to access a web page. Unfortunately, the site is returning an error message: Exception in thread "main" org.openqa.selenium.WebDriverException: com.gargoylesoftware.htmlunit.ScriptException: ReferenceError ...

Passing a variable between functions in JavaScript: Tips and tricks

let chart; let data = new Array(); function handleClick() { data = document.getElementById('graph:hi').value; alert(data); } let chartData = new Array(66, 15, 2.5, 21.9, 25.2, 23.0, 22.6, 21.2, 19.3, 16.6, 14.8); alert(chartData); jQuery ...

Conceal the name of a property when displaying it

I have a function that retrieves data from an interface, which includes a property called name. Currently, the output includes the property name, but I would like to remove it if possible. How can I achieve this? Here is the interface structure: export i ...

Tips for deploying a Nuxt application using an alternative command line interface

Despite my best efforts scouring the internet for a solution, I have yet to find a method to resolve my issue. The problem lies with my nuxt.js SPA being blocked by my company firewall when accessed by other users at the designated host/port. While servin ...

Finding the parent div in the handleChange function using React

I am facing a challenge with multiple dynamic divs, as their number depends on the filter selected by the visitor. This makes it impossible to use getElementById in this scenario. My goal is to modify the parent div's CSS when an input is checked. B ...