Unable to send a request to ASP.NET MVC action while utilizing AngularJS Routeprovider

I'm encountering a problem with the angular route provider. Here is the route configuration in app.js:

fmApp.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider){
 $locationProvider.html5Mode(true);
 $routeProvider.when('/', {
   templateUrl: 'staticViews/home.html,
   controller: 'fmController'
 }).otherwise({ redirectTo: '/' })
})

In my web.config file, I've set up URL rewriting to redirect all requests to index.html:

</system.webServer>
    <rewrite>
      <rules>
        <rule name="Rewrite Url to layout.html">
          <match url=".*\.html|css/|img/|js/|data/|lib/|templates/|favicon.ico" negate="true" />
          <action type="Rewrite" url="layout.html" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

Within index.html, there is a form that, when submitted, should make a request to an action on the server side using $http

$scope.submit = function(){
    $http({
      method:'post',
      url: '/api/test',
      data: JSON.stringify(
        angular.copy($scope.data)
      )
    })
}

The issue arises when attempting to request /api/test - it does not call the server-side (api-controller and test-action) but instead redirects to layout.html due to the rewrite in web.config. If I do not rewrite the URL in web.config and use

$locationProvider.html5Mode(true);
, a # will appear in the URL like this: http://localhost/#!/....

Answer №1

Seems like your API might be missing in the rewrite negate configuration. You could attempt adding it with:

<match url=".*\.html|api/|css/|img/|js/|data/|lib/|templates/|favicon.i‌​co" negate="true" />

Give that a try, maybe?

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

Recording the usernames of users in an array by utilizing the Userid feature in discord.js version 13

I am currently working on creating a field called "ReqUsers" in the object "commandInformation". I am trying to make it check if the message.author has the necessary permissions and/or roles, or if the message.author.id is included in the list of users spe ...

Sending the object context back to the controller callback from an AngularJS Directive

I am currently working on creating a modified version of ng-change with a delay feature for auto-saving changes. Here is the custom directive I have implemented: myApp.directive('changeDelay', ['$timeout', function ($timeout) { re ...

Tips for halting a MySQL database transaction within a NodeJS environment using expressJS

Recently, I encountered a coding challenge that involved managing a long database transaction initiated by a user. The scenario was such that the user inadvertently updated 20 million entries instead of the intended 2 million, causing a significant impact ...

"Failed authentication for client" in the testing environment of PayPal sandbox

I obtained the code from github and updated the Client ID & App Secret accordingly. It worked flawlessly. https://github.com/paypal-examples/docs-examples/tree/main/advanced-integration However, upon integrating the codes into my project, I encountered th ...

Navigating within a React application using React Router 2.6.0 by triggering a redirection within a click

Currently, I am experiencing an issue while utilizing react-router for constructing a login system with firebase and react. The desired functionality involves redirecting the user to the home page upon successful authentication of their username and passw ...

Why isn't my AJAX POST request to PHP functioning correctly?

It was all working perfectly fine earlier, but now something seems off and I must have made a mistake somewhere. I'm in the process of setting up a form where the information entered is sent via AJAX to my PHP file, which then outputs the username an ...

Building a web application with ASP.NET MVC4 that incorporates Angular.JS for Active Directory authentication

Currently, I am developing a web application using C# in combination with ASP.NET 4.5/Entity as the backend technology. Angular.js is being used to manage most of the front-end tasks and database queries. The ease of connecting to our active directory thro ...

AngularJS script to dynamically update a table upon selecting an option from the dropdown menu

Just starting out with Angularjs and facing a challenge. I have a table with a "Update" button on the UI and a drop-down option in the 3rd column. The requirement is, upon selecting an option from the drop-down and clicking the "Update" button, the values ...

Angular 2: Troubleshooting Issues with Observable Data Display

Looking to implement a RESTful call with Angular 2 that constantly updates whenever there are changes in the API. In my service, I've included an Observable to fetch data from the API: getData(): Observable<any[]> { return this.http.get(url) ...

Consecutive pair of XMLHttpRequest requests

Is it possible to create a functionality where the page index.php sends a Javascript variable called idToken, which is then received in another page called token.php using Javascript as well? In the token.php page, there will be more code that processes ...

Using Vue.js 3 composition, I have encountered an issue where the v-if directive is not functioning as expected on two separate div

Is there a way to seamlessly toggle sections of the app in Vue.js 3 composition using v-if? I managed to make it work with the vue.js 2 API option initially, but since transitioning to vue.js 3 API Composition, the section isn't hiding despite screenM ...

Count, copy, and validate a group of spans using Protractor with AngularJS

As a beginner in automated testing, Protractor, and angularJS, I am looking for guidance on counting and verifying text in a list. The list contains items like Attractions, Capacity, and Content, which inform the user of their privileges. Here is the rele ...

Battle of Kingdoms API ajax

When attempting to access Clash of Clans API information in this script, the following error is encountered: Refused to execute script from 'https://api.clashofclans.com/v1/leagues?authorization=Bearer%20eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiIsImtpZCI6Ij ...

When a barcode scanner is used, it will trigger a "keypress" event only if the user is currently focused on an input box. Is there a specific event to monitor when the user is not on an input

Scenario: In the development of my web application, I am utilizing a barcode scanner device that allows users to scan barcodes for navigation to specific pages. Challenge: The current barcode scanning device is set up to only trigger "keypress" events w ...

Tips for securely implementing JSON web tokens when integrating an external application with the WordPress REST API

I have a query regarding JWT. Let's consider this situation. A -> wordpress site with wp rest api enabled; B -> External application (for example, a simple javascript/jQuery app) Suppose I want to make a post request or create a new post on t ...

Using three.js to add an image onto an already existing object

I attempted to modify the appearance of an object by adding an image, but all my tests resulted in a white object... The goal is simply to apply an image to the entire object. Here is my test code: var obj = scene.getObjectByName('wall_20_118') ...

Ways to access the content of the chosen <td> element using Vue?

I have a dynamic table where I retrieve data using $.ajax() from a database. The content in the rows is editable, and I need to save the changes made with vue.js. After updating the content, an $.ajax() function with 4 parameters (name, client_id, url, and ...

The process of masking a video with alpha data from another video on canvas seems to be experiencing a

I have a unique setup on my page where I'm masking one video with another. Essentially, when the Play button is pressed, a second video slowly appears over the looping video in the background. This effect is achieved by using a black/white mask transf ...

What are the best techniques for optimizing loops when inserting data into a database?

Hi there! Currently, I am facing a challenge in inserting data from an XML file into my MySql database using Sails.js and waterline for the queries. In my database schema, I have two tables named Users and Pets. A user can have multiple pets, and a pet can ...

Enhancing Functionality: JavaScript customization of jQuery (or any other object's) function

Within this jsfiddle, the author has created a test for a custom knockout binding. A key aspect of the test involves extending jQuery. My question pertains to lines 30. $.fn.on = function(event, callback) { where it appears that any existing definition o ...