Issues encountered when trying to modify the Content-Type of POST requests using ngResource versions 1.0.6 and 1.1.4

Despite numerous attempts and trying various solutions found online, I am still unable to solve this issue.

Similar questions have been asked in the following places:

Various attempts have been made without success:

var app = angular.module('theApp', ['app.services']);


app
  .config(['$httpProvider', function ($httpProvider) {
    // Try (1): This doesn't work
    $httpProvider.defaults.headers.common['Content-Type'] = 'application/json;charset=utf-8';
    // Try (2): This doesn't work either
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/json;charset=utf-8';
  }])


angular.module('app.services', ['ngResource'])
  // Resource for Drupal system/connect.post API (via services.module)
  .factory('SystemConnect', function($resource, $http) {
    // Try (3): There's no way this should work. But what the hell let's try!
    $http.defaults.headers.common['Content-Type'] = 'application/json;charset=utf-8';
    $http.defaults.headers.post['Content-Type'] = 'application/json;charset=utf-8';

    return $resource('api/system/connect.json', {}, {
      post: {
        method: 'POST',
        params: { },
        isArray: true,
        // Try (4): This doesn't work either
        headers: { 'Content-Type': 'application/json;charset=utf-8' }
      }
    });
  });


function SomeCtrl($scope, SystemConnect) {
  // FAIL, this results in "406 Not Acceptable: Unsupported content type application/xml"
  $scope.user = SystemConnect.post();
}
app.controller('SomeCtrl', SomeCtrl);

It seems like others have managed to solve this problem before. Could someone please guide me on the correct approach?

PS: Interestingly, when running this code in Firefox, Angular uses 'Content-Type: text/plain' for the POST!?

Answer №1

I recall learning that when making header modifications in a post, it is necessary to include content for the changes to be accepted.

$scope.user = SystemConnect.post({});

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

What is the best way to retrieve the total number of nested objects within an object?

I'm trying to figure out how to get the number of nested objects in the object a. a = { firstNested: { name: 'George' } secondNested: { name: 'James' } } I thought about using .length, which is typ ...

Encountering a Type Error when using bootstrap-table and Angular for server-side pagination

Encountering an issue with Uncaught TypeError: bsTable.$s.$applyAsync is not a function. Code from the HTML page: <div ng-app="testapp"> <div ng-controller="MainController"> <table bs-table-control="dashboardTableControl"></tabl ...

Trouble with CSS transitions not functioning while altering React state

Each time a user clicks on an accordion, the content should smoothly show or hide with a transition effect. However, the transition only seems to work when opening a closed accordion, not when closing an already open one! To get a clearer idea of this iss ...

Tips for incorporating jQuery UI into Angular 6

No matter how many methods I have tried from StackOverflow, I cannot seem to get jquery-ui to work in my angular 6 component. Here's what I've attempted: I went ahead and ran npm install jquery jquery-ui to install both jquery and jquery-ui. ...

Prevent page from scrolling while md-menu is activated on IOS devices

Has anyone encountered the problem where md-menu allows scrolling behind the menu on iOS? I've found myself stuck in this predicament. Does anyone know of a solution to prevent the scrolling from happening? ...

Tips for displaying an element for each item chosen in a multi-select box

I currently have a situation where I am rendering for every selected element within my multiselect angular material selectbox. The rendering process functions correctly when I select an element, but the issue arises when I try to deselect one - it just ke ...

Tips on linking a condition-reaction to document.querySelector

I am struggling to connect the condition-reactions to the input id of passid. I am unsure where to place the document.querySelector() method in order to link the indexed conditions correctly. Below is the code snippet: <!doctype html> <html> ...

PHP - session expires upon page refresh

I'm in the process of creating a login system for my website and I've run into an issue with updating the navigation bar once a user has logged in. Every time I refresh the page, it seems like the session gets lost and the navigation bar doesn&ap ...

Using the bootstrap-select.js and ng-options to enable multi-select in Angular

Having some issues with a select multiple in Angular. Utilizing the bootstrap-select.js library, I need to assign values in ng-options, but when I add the multiple attribute it breaks the select functionality. <select ng-show="vm.showA ...

How can I efficiently create an editForm in Angular?

Within my database, there are numerous users, each with their own collection of recipes. Each recipe contains various properties and a list of ingredients. Take a look at the screenshot below: Recipe with all properties When a user goes to edit a recipe ...

The method Object.keys.map does not return the initial keys as they were in the original object

I'm retrieving an object from my database and it looks like this: {availability: null bio: null category: "" createdAt: "2020-10-13T13:47:29.495Z" email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfem ...

Firefox MediaSourceExtension now has enhanced mp3 support

Currently exploring the integration of adaptive and progressive audio streaming in the browser without the need for plugins. MSE is the HTML5 API that I've been anticipating, now available in FF 42. However, I'm encountering issues with audio fo ...

What is the correct way to import the THREE.js library into your project?

I recently added the three library to my project via npm. Within the node_modules directory, there is a folder named three. However, when I tried to import it using: import * as THREE from 'three'; I encountered the following error: ReferenceEr ...

Making a JavaScript script compatible with select drop down menus that can handle multiple selections

After searching through various sources, I came across two threads that address my question regarding the code to use. Despite finding solutions, I am struggling to understand how to implement the script. My limited experience with JavaScript is likely cau ...

Retrieve the result set rows and store them in an array as individual objects

I'm attempting to fetch data from a MySQL database using Angular and PHP. My Angular code looks like this: $http({ url: "http://domain.com/script.php", method: "POST", headers: {'Content-Type': 'applica ...

Vue: Conditionally importing SCSS when a component is instantiated

My goal is to import Material SCSS exclusively for my Admin component. While this setup works smoothly during local development, the issue arises when I deploy my site to Firebase hosting - the Material styles start affecting not just my Admin component, ...

Leverage scope variables and dynamic CSS properties when using ngClass

Is there a way to manipulate the CSS dynamically in HTML using a scope variable? <div ng-class="myClassScope, { 'dynamic-class': !ifIsNot }"> ...

Executing code within Vue js $set method's callback

For my v-for loop that generates a list of flights, I have implemented functionality where the user can click on a "flight details" button to send an AJAX request to the server and append new information to the results array. To update the view accordingly ...

Building routes for a stationary website with Angular

Currently, I am in the process of developing a static site using a combination of HTML, CSS, and JS along with nodeJS and express for server-side functionality... One challenge I am facing is setting up routes to display pages like /about instead of acces ...

How do webpack imports behave within a ReactJS project?

In my ReactJS project, I have utilized various 3rd party libraries such as lodash, d3, and more. Interestingly, I recently discovered that I did not explicitly write imports like import d3 from 'd3' or import _ from 'lodash' in all of ...