The Webmethod does not connect with Angular2, causing the entire page to display as raw HTML content

Here is a straightforward WebMethod:

[WebMethod(EnableSession=true)]
public static string Redirect()
{
    object a= HttpContext.Current.Session;
    return "yeppieee";
}

It is being called like this:

this.http.get(`http://localhost/MyApp/Pages/TestPage.aspx/Redirect`)
        .map(res=>res)    
        .subscribe((res) =>{                
                console.log(res);                                   
                },
                 (err)=>console.log(err),
                 ()=>console.log("Done1111111111")
        );

Despite all this, the debugger does not hit.

In the console, I only see Done1111111111. The network tab in the developer tools shows that the status of this request is 200, meaning OK.

So why isn't the WebMethod being executed?

Edit

I just discovered that the response is actually the entire HTML page. This can be seen in the console output.

Answer №1

Success! I was able to resolve this issue by modifying the request header to return JSON data instead of the entire HTML page.

let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
this.http.get(`http://localhost/HelixWebApp/Pages/AngularContainer.aspx/Redirect`, options)
    .map(res=>res.json())    
    .subscribe((res) =>{                
        console.log(res);                                   
        },
        (err)=>console.log(err),
        ()=>console.log("All tasks completed")
);

I also updated the WebMethod to allow for GET requests:

[WebMethod(EnableSession=true)]
[ScriptMethod(UseHttpGet = true)]
public static string Redirect()
{
    object a= HttpContext.Current.Session;
    return "Success message";
}

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

The RouteParams encounter a problem because it is unable to resolve all parameters

I'm encountering an issue with the RC3 router. The error message I am receiving is: Can't resolve all parameters for RouteParams: (?). Below is my code: //route.ts import {provideRouter, RouterConfig} from '@angular/router'; import {H ...

Transform JSON empty arrays into XML using Json.NET (Newtonsoft)

Considering the given JSON data: string json = @" { ""Identifier"": ""MyID"", ""Data"": [] }"; If I change it to XML using: XDocument xDocument = JsonConvert.DeserializeXNod ...

What is the reason for a char array displaying its contents on the console in C#, while string and int arrays do not?

Why is there strange behavior when using Console.WriteLine() with different types of arrays? When I use it with an int or string array, it prints (System.String[]), (System.Int32[]). However, when I do the same with a char array, it displays the content ...

What is the best way to create an instance method in a subclass that can also call a different instance method?

In our programming project, we have a hierarchy of classes where some classes inherit from a base class. Our goal is to create an instance method that is strongly-typed in such a way that it only accepts the name of another instance method as input. We d ...

"Error 404: The file you are looking for cannot be found on [custom company domain]. Please check

My attempts to retrieve a Google Drive file using its file ID with a service account in NodeJS have been unsuccessful. The requests are failing with an error indicating a lack of access: code: 404, errors: [ { message: 'File not found: X ...

Conducting a Kubernetes health assessment for a .NET 6 C# Console Application designed to handle messages from an Azure Service Bus Message Queue

Currently, I am in the process of transferring a .NET 6 C# Console App that handles messages from an Azure Service Bus Message Queue to function within Kubernetes instead of its original setup as a WebJob in an Azure Web App. My main goal is to have it run ...

Issues with multiple validators in Angular 8 intricately intertwined

I'm facing an issue with a reactive form control that has multiple validators. Despite defining the validation methods, the form is not being validated as expected. Below is the code snippet illustrating my attempted solutions. Method 1: civilIdNumbe ...

Developing a custom Tag Helper for HtmlHelper.Raw

Trying to incorporate a Tag Helper class into my asp.net core project that outputs the raw content. Below is my implementation: public class RawTagHelper : TagHelper { public RawTagHelper(IHtmlHelper _) { HtmlHelper = _; } privat ...

Instructions for inserting a hyperlink into a column of a table with the *ngFor directive in Angular2

I currently have a table with 4 columns: Name, ID, Department, and City. I am fetching both the row data and column data as an array from a TypeScript file and iterating through them using *ngFor. Below is a snippet of my code: <tbody> <tr *ng ...

Using React hooks with Material-UI: Snackbar displaying only on first occasion and not again

I have identified an issue that can be easily reproduced. Steps to replicate: Step 1: Start a react app and include material-ui in the project: prompt> create-react-app mui-test prompt> cd mui-test prompt> yarn add @material-ui/core @material-ui ...

Tips on leveraging separate files for classes in TypeScript

I'm currently working on developing a TypeScript application where each class is saved in its own .ts file. I prefer to use VS Code for this project. So far, my compile task seems to be functioning correctly (transpiling .ts files into .js files). How ...

Having trouble with your Kendo Grid not showing JSON data in your MVC

I am facing an issue with my controller where it is returning JSON data instead of populating the Grid, showing raw JSON text. This is my first time using Kendo and I am unsure of what the problem might be. Is the grid not being initialized properly? I hav ...

When using a function as a prop in a React component with Typescript generics, the type of the argument becomes unknown

React version 15 or 18. Typescript version 4.9.5. When only providing the argument for getData without using it, a generic check error occurs; The first MyComponent is correct as the argument of getData is empty; The second MyComponent is incorrect as t ...

Next.js Custom App now offers full support for Typescript in Accelerated Mobile Pages (

I am looking to implement AMP in my custom Next.js project using Typescript. While the official Next.js documentation does not offer support for Typescript, it suggests creating a file called amp.d.ts as a workaround. My application includes a src folder ...

Inadequate execution of a Faux Router

UPDATE: After following the advice provided in the solution below, I realize that I confused router testing methods. I have made the necessary changes to my code and the tests are now passing, but I am encountering this warning: WARN LOG: 'Navigation ...

Angular error: Attempting to reduce an empty array without an initial value

I am encountering an issue with my array being filtered and reduced. getPageComponents(title: string) { this.pageComponents = []; const pageBlock = this.pageComponents.filter((val) => { if (val.page_title === title) { retur ...

Issue with displaying an Asp.Net website within an iFrame

I need assistance with displaying an external site within an iframe. However, whenever I attempt to do so, I encounter the error message: "This content is not displayed in a frame". <div id="frameDiv" style="height: 900px;"> <iframe id="leftFrame ...

Tips for solving AJAX post with jQuery on an ASP.NET page

After some experimentation, I discovered that adding the line alert(file) after the ajax block of code allows the code to work properly. However, when I remove the alert(file) line, the code fails to work. Here is the functioning code: function Delete(f ...

Incorporating Redux into Angular 2 with SystemJS loading technique

I have been delving into learning Angular 2 and I am keen on integrating Redux into my project. Currently, I have set up my project using angular-cli on rc2 release. This is my systemjs configuration: /************************************************** ...

Is it Observable or Not? Typescript Typehint helping you determine

I'm facing an issue while writing a function with correct typehints. It seems to work fine with explicit type hinting but breaks down when trying to use automatic type hinting. Can someone please help me identify the error in my code and suggest how I ...