Having trouble accessing cookies in JavaScript on Internet Explorer 11?

I am facing an issue with my Angular application where cookies are not being read properly in Internet Explorer 11. The JavaScript code works fine on Chrome, but IE seems to be having trouble accessing the cookie data even though it is visible in the developer tools and not set to HTTP only. I have already checked that all cookies are enabled on IE.

If anyone has any insights or suggestions on why IE is behaving this way with my app, I would greatly appreciate the help.

Below is how I set the cookie in my controller:

HttpResponseMessage response = new HttpResponseMessage();
JsonMediaTypeFormatter json = new JsonMediaTypeFormatter();
response.Content = new ObjectContent<PvUser>(MyPOCO, json, "application/json");
var cookie = new CookieHeaderValue(cookieName, cookieValue);
cookie.Domain = Request.RequestUri.Host;
cookie.Expires = DateTimeOffset.Now.AddMinutes(20);
cookie.HttpOnly = false;
response.Headers.AddCookies(new CookieHeaderValue[] { cookie });

And here is the code snippet from w3schools that I am using to retrieve a cookie by its name:

function getCookie(cname) {
  var name = cname + "=";
  var ca = document.cookie.split(';');
  for (var i = 0; i < ca.length; i++) {
      var c = ca[i];
      while (c.charAt(0) == ' ') c = c.substring(1);
      if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
  }
  return "";
}

Answer №1

Despite grappling with much pain and confusion, I have discovered that this perplexing issue can be resolved by using HttpResponse instead of HttpResponseMessage.

HttpResponse response =HttpContext.Current.Response;
response.Clear();
response.SetCookie(cookie);
response.StatusCode = 200;// HttpStatusCode.OK;
response.ContentType = "applicaiton/json; charset=utf-8";
response.Write(jsonSerializer.Serialize(loggedInUser));
response.End();

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 adjust the color of a button element?

I've been troubleshooting the mouseover function of my JavaScript button object. The goal is to have a function call (specifically show()) within the object that detects the mouseover event and changes the button's color to grey. I suspect that t ...

Click on the checkbox to activate it using JavaScript

I'm trying to use JavaScript to toggle the checkbox status when clicking a button. The first button is supposed to uncheck the box and it's working correctly: function clear() { document.getElementById("check").checked = ""; } However, I al ...

What's the best way to align several buttons in the center of the screen horizontally?

I'm struggling to properly align divs with images inside another div. My goal is to insert buttons into my HTML and use jQuery to center them automatically, but I can't seem to get it right. You can view the fiddle I've created here: http:/ ...

Empty Ajax array sent to PHP

I am encountering an issue with my ajax form where the values in an array are coming out as null in my PHP response. I suspect that there may be a mistake in my .ajax call. Can anyone provide suggestions or insights on how to resolve this issue? My code sn ...

Using an alias to call a function defined in a separate module in TypeScript

The following code snippet is from the v4.js file located inside the uuid folder within Angular's node_modules: var rng = require('./lib/rng'); var bytesToUuid = require('./lib/bytesToUuid'); function v4(options, buf, offset) { ...

Tips for using Eval to assign a string to CssClass in .NET

After executing the code snippet below within an aspx page, everything works perfectly. <%# (DataBinder.Eval(Container.DataItem,"ExtendedProperties['ShowDetail']")) == null ? "show showdetail" : ( (bool)(DataBinder.Eval(Container.DataItem,"Ex ...

"Bower fetches and installs specific versions of packages from the repository

I'm currently using npm 3.3.6 and bower 1.6.8 on Windows 10. Whenever I attempt to install a package like jquery or framework7, it ends up downloading and installing an archived version of the package. Here's an example: > bower install jquer ...

Preventing Prepend Scroll: Tips and Tricks

Adding extra content to the top of the body can be frustrating, especially if it pushes everything down as you're trying to read. Is there a way to smoothly prepend this additional content without shifting the page layout, so that it seamlessly appear ...

Guide to correcting Form Data errors with XHR resulting in a 400 Bad Request

This is a straightforward piece of code I've written. It's simply a POST request to my API endpoint using FormData. Interestingly, the API is returning a bad request error for reasons unknown to me. When I tested the API with curl, everything wo ...

How to retrieve attributes of a model from a related model in Sails.js

Currently, I am utilizing the beta version(v0.10.0-rc3) of Sails.js and have updated the database adapter to PostgreSQL in order to enable associations via the Waterline ORM. My main focus is on creating a role-based user model for authorization based on d ...

Month and year selection feature in ExtJS 4 catalog

I recently came across an interesting Fiddle that featured a Month and Year picker for apps. After finding this Fiddle here, I noticed that it was built using Extjs 5.0, and it worked perfectly. However, when attempting to switch it to version 4.2.1, the l ...

Pair HTTP POST data with a Dictionary<string, string[]>

Encountering an issue with parameter mapping for an ASP.NET MVC action. When posting data from the browser, it appears like this: { "query" : "some search phrase", "faceting" : { "colors" : ["red", "blue"], "brands" : ["Fantastic"] } Th ...

Problem with integrating JSON information into a Jquery Datatable

I'm struggling to bind JSON data in order to create a data table. Here is the structure of my JSON: [ { "ID": 1, "Number": "2", "Name": "Avinash" }, { "ID":2, "Number":"21", "Name":"XYZ" }, { "ID": 3, "Numbe ...

Assign a class when a path is clicked using Leaflet.js

My goal is to apply a specific class to the clicked polygon by using the following code: function addClass() { function style(feature) { return { className: "active" }; } } function onEachFeature(feature, layer) { ...

Tips for personalizing text and icon colors in the TableSortText element of Material-ui

My Goal: I aim to empower users with the ability to apply customized styles to my EnhancedTable component by utilizing a styles object containing properties like headCellColor, headCellBackgroundColor, bodyCellColor, bodyCellBackgroundColor, and more. The ...

What is the best way to adjust a wide HTML table to make it narrow enough to fit perfectly within the screen width?

I need assistance with formatting an HTML table that contains multiple columns: <table id="req_header" border="2" style="overflow: auto;"> <tr> <th><i class="fa fa-solid fa-check"> ...

Having trouble importing the UpgradeModule from @angularupgradestatic in Angular version 2.2.1

I am in the process of updating my AngularJS (ng1) application to Angular 2 (ng2). My Angular version is 2.2.1. Upon importing UpgradeModule from @angular\upgrade\static, I encountered the following exceptions: Uncaught SyntaxError: Unexpected ...

Verify the username in the registration form before clicking the Register button

I have a PHP-based website and I want to implement a user registration form with unique usernames. How can I validate the username before submitting the request to the database for registration? ...

Issue encountered while rendering tables with Jquery-dataTables

Encountering an issue with jquery datatables when fetching data from a URL using ajax calls. Implementing the codeigniter framework, I utilize the datatables library to create datatable objects by invoking the echo $this->datatables->generate() metho ...

Performing updates on Meteor.users collection while handling a promise rejection (leveraging fcm-push with Meteor)

My current project involves the use of an NPM package called fcm-push in order to send FCM notifications to different mobile devices based on specific messages. Everything works fine when the message is successfully sent, but if the sending fails due to th ...