"Receiving a 200 OK response is mistakenly handled as an error by Dojo

I am utilizing Dojo's xhr function to send data to an ASP.NET MVC controller:

xhr.post("/MyController", {
       handleAs: "json",
       data: {
          contentIdentifier: 'abc123',
          language: 'en'
        }
     }).then(function (response) {
         console.log('Success.');
     }, function (err) {
         console.error(err);
     });

The controller is returning a response in plain text, and the server confirms with a status of 200 OK.

Despite receiving a successful response, the error handler always activates.

Upon logging the err object to the console, I observe something like this:

SyntaxError: Unexpected token S
message: "Unexpected token S"
response: { 
   options: TMP
   status: 200
   text: "Successfully pushed content to 1 instance(s)."

It appears that the issue lies in parsing plain text as JSON, causing it to break at the first letter "S" in "Successfully". Could this be what's causing xhr to throw an error?

The response header specifies a content type of text/plain

Answer №1

Oops!

The handleAs parameter as per the official Dojo documentation:

The handler used to manipulate the result data.

In simple terms, changing handleAs to "text" was the solution to the issue.

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

Quotes Envelop HTML Content

When utilizing a Rich Text Editor to save data into a SQL database and retrieve it using a Databinder to display the value within a div, I encountered an issue where the HTML code is enclosed in quotes, causing it not to display properly. Below is a snipp ...

Multiple occurrences of the cookie found in the document.cookie

My client is using IE9 and the server is asp.net (specifically a SharePoint application page). Within the Page_Load method of a page, I implemented the following code: Response.Cookies["XXXXX"].Value = tabtitles.IndexOf(Request.Params["tab"]).ToString(); ...

Can arrays be utilized as keys for Objects in JavaScript?

I am looking to create a dictionary in Javascript that can return the same result with different keys. Here is an example of what I have tried: var dictionary = { [CHW,CW] : { //CHW and CW refer to the same thing [F ...

Implementing auto-population of input field in Vue JS based on dropdown selection

I'm in search of a solution for automatically filling input fields in Vue.js. My form consists of various input types such as text, select dropdowns, and quantities. I want the vCPU, vRAM, and Storage Capacity fields to be filled with predefined value ...

Execute Validation Function on Every TextField and Radio Button

I'm new to Javascript and struggling to make my function work for both radio buttons and text fields. Here is the HTML code for the form: <form action="sendmail.php" method="post" name="cascader" onsubmit="prepareEventHandlers()" id="cascader"&g ...

Is there a way to calculate the overall total ($sum) of an array specifically from a nested field in an array structure?

I am looking to calculate the total sum of all elements in an array that is nested within my schema. Here is a snippet of the schema: const mongoose = require('mongoose'); let historySchema = new mongoose.Schema({ time: { type:String ...

Tips for extracting information from Firebase and showing it on a Google gauge chart

I'm having some trouble displaying data from Firebase on a gauge chart. I keep getting an error that says "Uncaught (in promise)". Here's the JavaScript code I've been working with: <script type="text/JavaScript"> google.ch ...

How can I retrieve the value of a <span> element for a MySQL star rating system?

Source: GitHub Dobtco starrr JS plugin Implementing this plugin to allow users to evaluate a company in various categories and store the feedback in a MySQL database. Enhancement: I have customized the javascript file to enable the use of star ratings fo ...

Establishing the Backbone router configuration

Having trouble identifying the issue with my Backbone router. Is there an error within this code block? The index route is functioning correctly, but the classes route doesn't seem to be triggered (for example, when I try navigating to a URL like loca ...

Output the JSON string retrieved from a specified URL

I'm currently working on using ajax to retrieve and parse JSON data from a specific URL. I am looking for assistance on how to store the parsed array into a variable. Any guidance or suggestions would be greatly appreciated. Thank you! function rvOff ...

Initiate a timeout mechanism upon accessing the URL

One approach that we have been using to trigger a URL through ajax involves receiving the response immediately after hitting the URL. However, there is now a requirement to implement a timeout for the response. This means that if the response does not arri ...

Creating a split-view menu in WinJS: A step-by-step guide

I'm attempting to create a versatile app using JavaScript. I followed a tutorial on the MSDN website, but for some reason, the "nav-commands" class isn't displaying when the splitview is opened. I have set closedDisplayMode = 'none'. & ...

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 = [ ...

Retrieve the complete line of text from both a textarea and an editable div

When I right-click inside a textarea, the entire line regarding the current cursor position is displayed. I am trying to achieve the same with an editable div, but it's not working. The console is empty, showing an empty string. Can someone please hel ...

Loading various choices through ajax and subsequently assigning a specific value

After the page loads, I have a select field called #parentCatSelect that generates values using AJAX / PHP through the function acLoadParentCategories. This function serves two purposes: creating a new part (which works as intended) and updating an existi ...

Trouble with posting a form submission after populating a select box dynamically using AJAX

Trying my hand at chaining select boxes in a web form using ajax for the first time and feeling a bit lost. The issue I'm facing is that when a user selects a country from one select box, an ajax request retrieves options for states and territories in ...

The Ajax HTTP request fails to send the document.cookie value when made to the same domain

Why is my XMLHttpRequest not sending the document.cookie? I am on the same domain! Here is the code snippet: document.cookie = "test=test"; var query_url = 'http://example.com/dosomething'; $.ajax({ url: query_url, type: & ...

Resetting an Angular Material select component

I've encountered an issue with Angular Material. Currently, I have a form with two select elements. The problem arises when I choose a value in one of the selects, it resets and loses its value. Could this be a bug? Or am I possibly making a mistake ...

Using jQuery to submit data via POST method without having the page reload

I have a link in the footer that, when clicked, jumps to the header with a # in the address bar. Is there a way to prevent this and stay in the footer? Here is the code snippet: <a href="#" class="clickIn" id="1" attrIn="Like"><span style="color ...

Utilizing JavaScript to eliminate objects from a collection and showcase a refreshed arrangement

On a simple webpage using bootstrap and js/jquery, data is fetched via ajax to create entries displayed to the viewer. The process involves: - making a request - parsing XML to create an array of objects with all the data - sorting by one property - cr ...