The server remains unreachable despite multiple attempts to send data using Angular's $http

I am encountering an issue with triggering $http.post:

app.controller('editPageController', function($scope, $routeParams, $http) {

    $scope.page = $routeParams.pageid;

    // retrieve page data from the server
    $http.get('/pages/' + $scope.page).
        success(function(data, status, headers, config) {
            $scope.Name = data[0].name;
            $scope.Content = data[0].content;
            $scope.Location = data[0].location;
        }).
        error(function(data, status, headers, config) {
            alert("Could not retrieve the page from the server");
        });

    // save page data on the server
    $scope.saveEditPage = function() {

        var postOBject = {Name: $scope.Name, Content: $scope.Content, Location: $scope.Location};
        $http.post('/pages/' + $scope.page + '/edit', postObject).
            success(function(data, status, headers, config) {
                alert("Success");
            }).
            error(function(data, status, headers, config) {
                alert("Could not edit the page on the server");
            });
    };

});

The template code:

<script type="text/ng-template" id="editPage.html">
    <h1>Edit page:</h1>
    <form ng-submit="saveEditPage()">
    <p>Name:</p>
    <input type="text" ng-model="Name" value="{{Name}}">
    <p>Content:</p>
    <textarea ng-model="Content">{{Content}}</textarea>
    <p>Location:</p>
    <input type="text" ng-model="Location" value="{{Location}}">
    <p><input type="submit" value="Save"> <input type="button" value="Cancel" ng-click="$back()"></p>
</form>

Unfortunately, the $http.post is not getting triggered. I have attempted wrapping the post call around $scope.$apply without success.

How can this be resolved?

Thank you

EDIT: RESOLVED

Answer №1

Remember, JavaScript variable names are case sensitive. In this case, you declared postOBject but tried to pass postObject.

An error occurred: ReferenceError: postObject is not defined

Once I corrected the typo, everything worked as expected for me.

By the way, I suggest using an IDE with static analysis feature - it can help identify undefined variables right away. Additionally, tools like Firebug or Chrome DevTools javascript console are extremely helpful in JavaScript development.

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 HTML slideshow is not automatically showing up as intended

I need to make a few adjustments to the picture slideshow on my website. Currently, all the pictures are displayed at once when you first visit the site, and it only turns into a slideshow when you click the scroll arrows. I want it to start as a slideshow ...

Using the object value to map an array and populate the resulting elements

i have a function const [jobs, setJobs] = useState([]) const addJob = (title, role) => { const newJobs = [...jobs, { title, role}] setJobs(newJobs) } whenever a form is submitted, the addJob function creates state data containing ...

Create your masterpiece on a rotated canvas

My goal is to draw on a canvas using the mouse, even after rotating and scaling the canvas container. The issue I am facing is that the mouse coordinates get affected by the rotation and scaling, making it difficult to draw correctly. I have tried switch ...

I am looking to automatically add a line of HTML text to the notifications list without requiring the user to manually refresh the page

Having trouble articulating the correct term or language I need, making it difficult to search. Q: How can I push a line of text from the server side to the user? I am developing an MVC 4 application using asp.net and c# in .net4. One page will feature a ...

Which is better: Utilizing Ajax page echo or background Ajax/direct HTML manipulation?

I am facing a dilemma and I could really use some guidance. Currently, I am in the process of developing an ordering system using PHP/Smarty/HTML/jQuery. The main functionality revolves around allowing sellers to confirm orders on the site. My goal is to ...

The TablePagination component from Material Ui is not functioning properly with my specific array of objects

My TablePagination is not updating the rows on each page of my table and not even limiting the rows with the row filter. I suspect this may be due to the way I'm building the table using an array mapping like this: 0: [name: Robert, age: 15, ...

Yesterday Box: Creating and Launching URL with Today's Date via Javascript Bookmarklet

Is it possible to create a simple bookmarklet that generates a URL with the current date and opens it automatically in the browser? This feature could be useful for creating URLs for prefilled forms or implementing something like yesterbox for web-based e ...

There seems to be an issue with the functionality of the Bootstrap Modal

I've been working on incorporating bootstrap login Modal functionality into my code, but for some reason, the screen is not displaying it. Here's what shows up on the screen: The red box in the image indicates where I am expecting my Login Modal ...

Displaying content on a webpage using PHP, AJAX, and HTML

Looking to update my current form setup. I have a basic Form below: <form action="" method="POST"> <input type="button" value="Generate Numbers" onclick="on_callPhp1()"/> </form> Accompanied by this javascript code: <script type="te ...

While iterating through a dynamically generated JSON data array, omitting the display of the ID (both title and value) is preferred

I am working with a JSON data Object and using $.each to dynamically retrieve the data. However, I want to display all values except for one which is the ID. How can I achieve this and prevent the ID from being displayed in the HTML structure? Thank you. ...

Issue encountered when making API requests in JavaScript that is not present when using Postman

Currently, I am developing a web application using express and one of the functionalities is exposed through an API with an endpoint on 'api/tone'. This API acts as a wrapper for one of Watson's services but I choose not to call them directl ...

Hiding Div with JavaScript (Quick and Easy)

Hey there, I'm looking to make regStart and regPage alternate visibility based on a click event. I'm still getting the hang of JavaScript so any simple answers would be appreciated. <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/x ...

Is it possible for an <input> tag in PHP AJAX to have an "action" attribute similar to a <form> tag?

Forms typically use an action attribute to specify where the data should be posted, such as a .php file. Do <input> elements have this action attribute as well? The answer is likely no, but I am curious to understand what concept I may be misunders ...

Title Tooltip Capitalization in Vue.js: A Guide

Is there a way to change only the first letter of the title tooltip (from a span) to be capitalized? I attempted using CSS with text-transform: capitalize;, however, it didn't have the desired effect. <template lang="pug"> .cell-au ...

Tips for utilizing the loadDataWithBaseURL() method to load CSS and JS files directly from an SDCARD

I am facing an issue with loading files from SDCARD onto a webview. The .html, .js, .css files are stored on the SDCARD in my scenario, and the html file content is encrypted. The steps I follow to load the html file are: Read file content Decrypt all co ...

The initial io.emit message seems to always be delayed or lost in transmission

io.on('connection',function(socket){ console.log('connected'); socket.on('disconnect',()=>{ console.log('a user disconnected'); }); socket.on('sendMessage',function(data){ const message = data.te ...

A beginner's guide to retrieving random names and images from JSON data for every object using JavaScript

I have a creative idea for a random ruffling game, where it picks a user's name and image randomly. However, I'm facing an issue with ensuring that each image matches the correct user when selected randomly. I want to make sure that every objec ...

Incorporating external content to make it easily discoverable by Google for optimal SEO performance

I am currently working on a project that involves loading external content onto a customer's site. Our main goal is to provide the customer with a simple inclusion method, such as a one-line link similar to Doubleclick, without requiring any server-si ...

Input field in a tableview

I have a ListView that features a dropdown, 4 textboxes and buttons. My goal is to only show the fourth textbox (enclosed in a span) when the dropdown value in each row of the ListView is set to 2. For instance, if there are 5 records being displayed in t ...

Using jQuery to select elements with specific innerHTML values

$('.select option:selected[innerHTML="5"]') In this case, I am attempting to choose an option that is currently selected and has innerHTML that perfectly matches a specific string (for example: "5") For reference, here is a fiddle link: http:// ...