The issue of the callback not being triggered by the sinon.js fakeServer when making a $.ajax call was encountered

I am currently working on a jasmine specification that involves testing a jQuery plugin I created:

describe "plugins", ->
    beforeEach ->
    @server = sinon.fakeServer.create()

    afterEach ->
    @server.restore()

    describe "reviewStatus", ->
    it "should attach dates to content", ->
      @server.respondWith("GET", "/GeneralDocument.mvc.aspx/GetDocumentParent?typeName=ncontinuity2.core.domain.Plan&documentParentUid=45f0bccb-27c9-410a-bca8-9ff900ab4c28d",
        [200, {"Content-Type": "application/json"},
        '{"ReviewDate":"22/09/2012","AcknowledgedDate":"05/07/2012"}'])   

      $('#jasmine_content').addReviewStatus('ncontinuity2.domain.Plan', "45f0bccb-27c9-410a-bca8-9ff900ab4c28")     

      @server.respond()

      expect($('#reviewDateTab').find("strong").eq(0).length).toEqual(1)

The function addReviewStatus in my jQuery plugin is designed as follows:

do($ = jQuery) ->
    $.fn.extend
        addReviewStatus: (type, uid) ->
            ele = @

            reviewData = null           

            getJSON '/GeneralDocument.mvc.aspx/GetDocumentParent', {typeName: type, documentParentUid: uid}, 
                                (document) ->
                                    console.log('document = ' + document)
                                    compileTemplate(ele, document)
                                (response) ->
                                    showErrorMessage response.responseText
#etc., etc.

In the getJSON method utilized by the plugin, an issue arises where the anonymous function callback is not being triggered. Additionally, the call to $.ajax returns a 404 not found error. Can anyone provide insight into what may be causing this problem?

Answer №1

If Sinon's fakeserver receives a 404 error, it means the requested URL does not have a predefined response.

It seems like your issue may be related to not using the precise URL specified in the respondWith() method. Additionally, there could be a restriction on the length of URLs in Sinon, although this is uncertain.

Answer №2

I have encountered a similar issue as well. It appears to be connected to disabling the cache in the AJAX request. If I find a solution, I will update this post. You might want to test by disabling the cache and checking if it resolves the problem. The necessity of the cache for this operation is unclear to me.

Regards, Ronan

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

Best Practices for Retrieving Data with Django - Post Requests

I'm attempting to use Ajax to send a JavaScript array to Django. Here's my code: document.getElementById('input-generate').onclick = () => { // Get the token const csrftoken = document.querySelector('[name=csrfmiddlewar ...

The MUI Slide Transition experiences a malfunction when using multiple Slide components simultaneously

I'm having trouble getting the animations to work for each mui Slide when the button is clicked. It seems like they don't want to cooperate and work together. Oddly enough, if you disable one of the slides, the other one starts working fine, but ...

Issues with injection of angularjs, sockjs, and angular-sockjs are causing functionality to not

As a newcomer to technologies like angular, sockjs-client, and cyclone, I've encountered an injection issue while attempting to utilize a component created by bendrucker. The component in question can be found at this link: https://github.com/bendruck ...

What is the correct way to invoke a function from a different file?

Having trouble calling a function from another file in my JS code. I am unable to call a function from another js file. I suspect there is an issue with linking the two files. Code snippet from my first JS file const { response } = require('expre ...

What is the best way to send data from a child functional component to a parent class component?

I am facing a situation where I have a parent class component and a child functional component. While there are examples available on passing values from a child class component to a parent class component, I am wondering how to pass a value from this type ...

Create your own AngularJS directive for displaying or hiding elements using ng-show/ng

Caution: Angular rookie in action. I'm attempting to craft a personalized widget that initially displays a "Reply" link, and upon clicking it, should hide the link and reveal a textarea. Here's my current progress, but unfortunately, it's n ...

Incorporating PayUMoney into MVC and utilizing ajax for seamless integration

Whenever the user clicks on a button, I want them to be redirected to the PayUMoney gateway. If the payment is successful, then the user's data should be stored in the database and they should be redirected to a success page. Otherwise, they should be ...

The function named updateContact is not defined, even though it has been declared

Presented below is Angular code snippet: @Component({ selector: 'app-detail-view', templateUrl: './detail-view.component.html', styleUrls: ['./detail-view.component.css'] }) export class DetailViewComponent implements O ...

How do I ensure my object is fully constructed before sending it in the response using NodeJS Express?

Currently, I am in the process of constructing a result_arr made up of location objects to be sent as a response. However, my dilemma lies in figuring out how to send the response only after the entire array has been fully constructed. As it stands, the re ...

Creating a state in React and populating the rows of a Material-UI table with fixed data

I have implemented a table in the UI using the material-UI Table component. Initially, I added static data without using the state property of react. Now, I am looking for a way to assign static data to table rows by utilizing the state property. The code ...

Mapping fields in Spring to JSON can be tricky, especially when dealing with fields that can be

In my object, there is a String value field which can contain either true or false. The issue arises when Spring/Jackson maps this to value: "true" as a String, causing problems with certain angularjs ng-model mappings that expect a boolean. Is there a way ...

Uploading an image without the submit event is not possible with AJAX

Looking for a way to upload images asynchronously using AJAX? I've scoured the internet for solutions, tried various combinations of codes, but none seem to work without a submit event. I want to stress that I can upload images using a button that tri ...

The error message "Evaluating this.props.navigation: undefined is not an object" indicates that there

In the navigation bar, I am trying to keep a touchable opacity at the top right. When this touchable opacity is pressed, I want to redirect the user to the home page. constructor(props) { super(props); this.state = { stAccntList: [], ...

The ng-click event is not triggering the Controller Function as expected

This is the Code for My View <div ng-controller="signupCtrl"> <ul class="list-group" > <li class="list-group-item"> <div class="form-group"> <input type="text" ng-model="signupCtrl.firstName"> ...

Refreshing only parts of an ASP page with ajax for a seamless user

How can I implement a partial page refresh using Ajax in cshtml? In my current setup, I have a table on the index page with rows representing programs (batch files) and a corresponding "Run" button. Below the table, there is a section designated for progr ...

Verify the presence of plain ASCII text

I need to verify if the file uploaded is in ascii plain text format. Can you suggest a method? $("#my_file").change(function(){ //alert if file is not ascii plain text }); <input type="file" name="my_file" id="my_file" /> ...

Tips for turning off hash-based redirection in AngularJS

Here is a specific URL: http://www.something.com/sometest/test/#registration Some code has been written based on #registration for Internet Explorer. However, when using AngularJS, it redirects to the following URL, which disrupts the logic: http://www ...

What is the process of creating a MaterialUI checkbox named "Badge"?

Badge API at https://material-ui.com/api/badge/ includes a prop called component that accepts either a string for a DOM element or a component. In my code: <Badge color="primary" classes={{ badge: classes.badge }} component="checkbox"> <Avatar ...

What could be the reason for my select list not showing up?

Hello fellow developers, I am currently working on creating a dynamic tablerow that allows users to fill in input fields and select options from a list for each cell. While the input fields are functioning properly, I am facing an issue with displaying th ...

My JavaScript code is being executed before Chrome Auto-fill

I have successfully created form input elements in Chrome that display a floating label when focused. However, I am encountering an issue when the browser autofills the username and password fields with yellow prefilled text. The JavaScript for the float ...