Transforming AngularJS $http POST requests into GET requests

I came across a scenario where I needed to update the logo path in my application. So, I defined a route like this:

Route::post('/updateLogo', 'CaptivePortalController@updateLogo');

After defining the route, I made a POST request as shown below:

$http({
    method: 'POST',
    url: '/updateLogo',
    headers: { 'Content-Type': undefined },
    transformRequest: function (data) {
        console.log("data coming into the transform is ", data);
        var formData = new FormData();
        formData.append("company_logo_path", data.files);
        console.log($scope.files.company_logo_path);
        return formData;
    },
    data: { files: $scope.files.company_logo_path }
})

.then(function successCallback(response) {
    console.log("success");
    console.log(response);
    $('.save-fade').delay(500).fadeOut(1000);
}, function errorCallback(response) {
    console.log("fail");
    console.log(response);
});

However, when I tried to upload the file and submit the form, I encountered an issue:

The network tab in Chrome Dev Tools showed a 405 error, indicating that a POST request was not allowed.


Upon further investigation, I discovered the error message:

MethodNotAllowedHttpException in RouteCollection.php line 218:

https://i.sstatic.net/FRyOR.png

I understand that I shouldn't be making a GET request to a POST route. However, I'm confused as to why it's being treated as a GET request instead of a POST. Any insights on what could be causing this issue?

Request URL:http://l.ssc.com:8888/en/updateLogo
Request Method:GET
Status Code:405 Method Not Allowed
Remote Address:127.0.0.1:8888
Referrer Policy:no-referrer-when-downgrade

Could anyone point out what mistake I might have made here?

Any suggestions or hints would be greatly appreciated. Thank you.

Answer №1

It appears that there is a re-direction occuring in this scenario. For more information, please visit: $http.post() method is actally sending a GET

We recommend verifying your route configuration on the server to ensure it matches the request you are making.

If you are requesting '/myroute' but have defined the route as '/myroute/', there may be a redirect happening to '/myroute'.

Typically, all re-directions happen with a GET request. If the route does not allow GET requests, it could result in a 405 error.

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

Error: Surprising token found in ReactJS application on CodeSandbox

Encountering an unexpected token on line 5 error in my ReactJS project when testing it on CodeSandbox. Interestingly, the app runs smoothly without errors on my local machine. import React, { Component } from 'react'; import Header from ' ...

User authentication on AngularJs is only initiated on the second interaction

My personally built AngularJs user authentication system is experiencing an unusual issue. While everything seems to be working fine - I can login, check access for specific pages, etc. - there is a strange behavior with the token authentication. It seems ...

What steps do I need to take to have Greasemonkey execute a function upon the completion of an AJAX call

Whenever I select the trending tab on YouTube, an AJAX call is triggered to retrieve the latest trending videos. I have developed a custom script that filters out any videos I have already watched. Although this script successfully applies to the homepag ...

"message": "Please provide a valid user path.",

When attempting to store the user in the database, the response indicates that the "user" path is required even though the user value is present in req.body and logged for verification purposes. However, the issue persists with storing it in the database. ...

Trigger an event click once a bootstrap table has been successfully reloaded

In my function, I have an AJAX call and in the success function, I refresh the Bootstrap table. After the refresh, there is a trigger command that I want to execute only when the refresh is completed. How can I achieve that? success: function (result) { c ...

Interacting between frames with jQuery

I have main_page.htm with the following frameset structure: <frameset rows="30,*" frameborder=0 border=0> <frame name="top_frame" src="top.htm"> <frame name="bottom_frame" src="bottom.htm"> </frameset> The content in ...

Successfully resolved: Inability to dynamically adjust button color according to its state

Currently I am working on a feature where a button changes color when it is disabled, but also has a custom color when enabled. Here is the code snippet I am using: Despite setting blue text for the button, it remains blue even after becoming disabled. Ho ...

An essential component of Chai assertion is the inclusion of "or"

While attempting to iterate through a list of elements, I am looking to verify that the body contains either of two values. However, the current assertion method only allows me to check for one value: expect(cellText).includes(toDateFormat); Is there a wa ...

Can you explain the significance of 1x, 3x, etc in the Karma code coverage report for Angular unit testing?

I am a beginner when it comes to Unit Testing in Angular. Recently, I set up karma with code coverage using angular-cli. After running the command ng-test, I checked out the code coverage report and noticed references like 1x, 3x, etc. next to my code line ...

I am having trouble getting text to display properly in a jQuery Mobile dialog

I am attempting to dynamically set the header and button texts using JavaScript, but unfortunately it's not working as expected. To demonstrate the issue, I have added my code on jsfiddle: http://jsfiddle.net/tMKD3/8/. Here is the HTML code: <bod ...

Obtain a numerical output from a selection of numbers

I am facing a simple problem that I can't solve due to my lack of knowledge and have not been able to find any solution online. What I want to achieve is: Create a "value 1" from an array of integers. Generate a random "value 2". Check if the rando ...

Continuously make Ajax requests to populate numerous div elements

There are two div elements present on my webpage. <div id="1"></div> <div id="2"></div> I am attempting to dynamically populate these divs using the following php call. <script> var queries = ["SELECT * from table1", "S ...

Successive Alerts with Material-UI and React-Redux

After realizing that having multiple snackbars scattered across different components was messy, I decided to revamp my app by creating a single custom component that can be called using Redux to display any type of snackbar needed. Desired outcome: I exp ...

When I trigger a function by clicking, I also set a timeout of 1 second. Is it possible to deactivate this timeout from within the function itself?

One button triggers a function with a timeout that repeats itself upon clicking. Here's the code snippet: function chooseNum() { let timeout = setTimeout(chooseNum, 1000); } To disable this repeating function, I attempted another function like so ...

Sorting for parents and their children

Sorting a table with parent- and child-rows can be tricky. The sorting should always be based on the parent rows, but the child rows need to follow immediately after their respective parents. Here is an example of the data format: [ {name: 'xxx' ...

Incorporate an icon into a text input field using Material UI and React

I have a form with a text input element: <FormControl className='searchOrder'> <input className='form-control' type='text' placeholder='Search order' aria-label='Search&ap ...

Avoiding Memory Leaks and Managing JS Heap Size While Polling Large Amounts of Data Every 5 Seconds with Vuex

I'm currently working on a Vue application that utilizes Vuex for state management. Within this application, I have several store modules set up at the root level. Periodically, every 5 seconds, I retrieve a large amount of data and update the store s ...

Utilizing AngularJS to connect a dynamic result array to a table with varying layouts

I am struggling to bind a dynamic array result with a table using Angular JS in a different layout. Despite multiple attempts, I have not been successful in achieving the desired outcome. Any help or guidance would be greatly appreciated. var arr = [ ...

Differences between addEventListener and jQuery's on method, as well as form.submit and jQuery's

I encountered a situation where callbacks registered using jQuery's on method were not being called when trying to trigger form submission with the HTML Form element object instead of jQuery. After some testing, I discovered that changing the code to ...

Use Node-RED to fetch JSON or CSV data and store it in InfluxDB

Versions Node-RED v0.16.2 InfluxDB v1.2.2 Grafana v4.2.0 Ubuntu 16.04.2 I'm looking to access weather data from a local official weather station. The options available are in either csv or JSON format. I am attempting to retrieve the JSON feed us ...