Direct ajax requests to a different path other than initially specified when sending the call

My C# MVC project is hosted on abc.com. When making an Ajax call for a resource on Controller:Home and Method:ClientInfo, the URL looks like this:

url: '/Home/ClientInfo'

For testing purposes, I deployed the same project on xyz.com/web. The home page in this case is xyz.com/web. However, when Ajax calls are made for the resource Controller:Home and Method:ClientInfo with the same URL '/Home/ClientInfo', it returns a '404 Not Found' error because the URL should be something like this:

url: '/web/Home/ClientInfo'

One solution would be to manually edit all Ajax URLs throughout the project, but that doesn't seem like the right approach. I have a common layout - can I write JavaScript code there to route all URLs under the directory '/web', or is there another way?

*Note: I do not have control over the web.config of xyz.com to handle the calls and write rules.

Please provide suggestions.

Answer №1

If you want to streamline your API routes in JavaScript, consider creating a separate file dedicated to exporting these routes for easy access and modification.

For instance using ES6

Create a file named routes.js with your API routes:

export const API = '/web/';
export const CLIENT_INFO_API = `${API}home/clientinfo`;

Then, when you need to utilize these routes in other files:

//Import the routes file
import * as ROUTES from 'routes.js';

//Access the routes
ROUTES.API
ROUTES.CLIENT_INFO_API

Alternatively, using ES5 syntax

Create a file named routes.js with your API routes:

var routes = {};
routes.API = '/web/';
routes.CLIENT_INFO_API = routes.API+'home/clientinfo';

module.exports = routes;

Then, when you need to use these routes in other files:

//Require in the routes file
const ROUTES = require('routes.js');

//Access the routes
ROUTES.API
ROUTES.CLIENT_INFO_API

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

Incorporating a protected Grafana dashboard into a web application

I am looking to incorporate Grafana into my web application using AngularJS. The main objective is to allow users to access the Grafana UI by clicking on a button within my application. Setting up an apache reverse proxy for Grafana and ensuring proper COR ...

Developing a table with JavaScript by parsing JSON data

Starting off, I am relatively new to working with JavaScript. Recently, I attempted to generate a table using data from a JSON file. After researching and following some tutorials, I successfully displayed the table on a web browser. However, I noticed tha ...

Remove several elements from an array within the state of a React component using values from a second array

Here is an array let removed = [ {id: '123', name: 'Something'}, {id: '321', name: 'Something1'} ]; and I also have this piece of code this.setState({ config: { ...this.state.config, ...

Tips for creating a pop-up window on an ASP.NET web form

I am looking to incorporate a popup window as a child of my primary asp.NET form. The popup window will allow users to input information using a dropdown list. When the popup window appears, it will take focus and disable the main window. ...

Using .NET to Monitor GTalk Instant Messages

Is there a way to track and respond to Gtalk instant messages in .NET? I am looking to create a program that will run when receiving specific arguments as instant messages from a designated Google ID, then send a response back to the sender. Do you think t ...

React fails to acknowledge union types

I have the following types defined: export enum LayersItemOptionsEnum { OPERATOR, HEADER, } type sharedTypes = { children: string | ReactElement; }; type LayersItemStatic = sharedTypes & { label: string; option: LayersItemOptionsEnum; }; t ...

TimeoutException Automatically Triggered by Selenium WebDriver in C#

We are in the process of transitioning our automation scripts from Selenium RC to WebDriver. I have encountered a few challenges with WebDriver, particularly when TimeoutExceptions are thrown immediately after any changes are made on the web page being tes ...

Utilizing Jquery select2 to enhance user experience by organizing JSON data with optgroup and

Currently, I am working with select2 in conjunction with spring mvc. The data I need to display is retrieved from my controller and should be displayed as options. However, I would like these options to be grouped within optgroups while also incorporating ...

Utilize an index to retrieve the data stored within a div that consists of numerous rows (divs)

I have a main container with several rows nested inside, here is an example: <div id="containerDiv1"> <div class="paramRow"> <input type="text" value="Foo" id="param1" /> </div> <div class="paramRow"> ...

Adjust the sliders according to the current time

I am looking to display different sliders based on the time of day. For instance, 'slider set 1' from 0-9am, 'slider set 2' from 9am-12pm, and so forth. I am new to java script and need assistance in solving this challenge. Below is the ...

Tips for structuring JavaScript code within ASP.NET MVC projects

We're looking to enhance the functionality of a view page with some Ajax functions. This means that when a user clicks on a delete or insert button, an Action will be triggered to carry out a CRUD operation in the background, followed by refreshing th ...

Modify the Google Translate dropdown using programming techniques

Recently, I attempted to integrate the Google Translate dropdown feature into a website using the following code snippet: function googleTranslateElementInit() { new google.translate.TranslateElement({ pageLanguage: 'en' }, 'google ...

The art of designing Mongoose and Router files

Let me share my directory structure with you: bin/ www models/ myMongooseModel.js public/ ... routes/ index.js anotherroute.js views/ ... app.js package.json In my app.js file, I have configured some settings using app.set and app.use comman ...

Implement a FOR loop in conjunction with an AJAX request

My goal is to send an AJAX request with multiple form fields and use an array for validations. I would like to also pass these values via AJAX: Although I haven't used a for loop in JavaScript before, it looks familiar. The current approach of the l ...

Skybox images failing to display

Learning THREE.js has been quite challenging for me, especially when trying to display a simple skybox. Despite attempting various strategies, I have not been successful due to insufficient documentation and multiple releases. This struggle has left me fee ...

Can one interact with PDF forms using PHP or JavaScript?

After spending a day digging into possible methods for manipulating PDFs with forms, I am still on the search. I have come across a PDF where users can fill in data, save it, and send it to me. The issue arises when some users are unfamiliar with Acrobat R ...

Tablesorter fails to initialize following Ajax update of HTML content

I am currently utilizing Mottie's Tablesorter jQuery plugin which can be found at this link. As part of my implementation, I am incorporating the pager plugin with server-side processing. The HTML structure is being constructed on the server end and ...

Collect data from a third-party website that necessitates the activation of JavaScript

Given that phantomjs has been abandoned, I am exploring alternative methods. For instance, using chrome-webdriver may not be ideal as it cannot operate on a remote host like heroku. Is there a way to scrape a website that relies on JavaScript being activa ...

Using jQuery to select and animate several elements simultaneously without repeating the animation

Issue Summary (Fiddle): I'm trying to make all elements fade out simultaneously when clicked, but the current code causes the target to trigger three times resulting in three alert() calls. I want them to fade together without using a wrapper element ...

What is the best way to make my navbar reach the edges of the screen?

Currently working on a regular navbar, but aiming to have the logo and links span the entire container instead of starting in the middle. I'm seeking an easy solution that won't push out my links when applied. https://i.sstatic.net/nTFJ6.png ...