Bug Alert: Incompatibility between Angular $resource and PHP causing issues with Update and Delete functionalities

As a newcomer to both AngularJS and PHP, I have been struggling to find comprehensive documentation on using $resource to update records in a database. While I did come across a helpful tutorial here that covers most aspects of $resource usage, I am having difficulty with updating and deleting records in the database.

Below is my code for the resource service:

var configServices = angular.module("configServices", []);
configServices.factory('Category', ['$resource', function($resource){
 return $resource("/configurator/api/categories/:id", {id:"@category_id"},
{update:{
mothod: 'PUT',
isArray: false
  }
 })

}])

This is my service code:

  this.updateCategory = function(_data){
var newUpdate = Category.get({id:_data.category_id}, function(result){
  newUpdate.data = _data;
  newUpdate.$update(function(res){
    console.log("Got respose from update call" + res);

   })
 })
}

While I have been following the tutorial closely, I am still unable to successfully update or delete records in the database. Any assistance in identifying where I may be going wrong would be greatly appreciated.

PHP:

private function updateCategory($data){

    $_id = implode("", $this->args);
    $query = "UPDATE cg_category SET category_name = '$data[category_name]' WHERE category_id = '$data[category_id]'";

    $result = mysqli_query($this->db, $query) or mysql_error();

    if($result)
    {
        $this->response($this->json(array("Success" => true)), 200);
    }

}

Answer №1

After encountering a bug in the Angular $resource class that was causing PUT requests to also send GET requests, I decided to switch to using the $http put method instead. The change has proven to be effective and my code is now functioning correctly.

Below is the updated code using $http:

this.updateCategory = function(_data){
$http.put("/website/api/categories/"+_data.category_id, _data).success(function(res){
  console.log("Successful PUT request: " + res);
 })
}

I am open to feedback and would love to know if you have found a solution for making the $resource class work with the PUT method.

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

Obtaining unique attributes from Laravel's many-to-many relationships

Currently, I am in the process of developing a module for my laravel web application that allows users to create multiple projects. These projects can have multiple contributors who need to be approved by the owners. The functionality for owner users has b ...

What could be causing me to receive 'undefined' and an empty array[] when using Promise.all with JavaScript async operations making calls to Azure APIs?

In my personal project utilizing Azure AI APIs and Node.js/Express,, I am working on handling a get request to a /viewText route by extracting text and key phrases from an uploaded image/document. Below is the code snippet that should log this data to the ...

Retrieve the array element that is larger than the specified number, along with its adjacent index

Consider the following object: const myObject = { 1: 10, 2: 20, 3: 30, 4: 40, 5: 50, }; Suppose we also have a number, let's say 25. Now, I want to iterate over the myObject using Object.entries(myObject), and obtain a specific result. For ...

Executing a Knex RAW MySQL query to insert new records into a database table

As someone new to working with MySQL, I have previously used PSQL in a similar manner. However, the following code is generating an error. return await db .raw( `INSERT INTO users(firstName, lastName, email, ...

What is the best way to invoke a TypeScript function within a jQuery function?

Is it possible to invoke a TypeScript function within a jQuery function? If so, what is the correct approach? Here is an example of my component.ts file: getCalendar(){ calendarOptions:Object = { height: 'parent', fixedWeekCount : ...

Is there a way to prevent undefined properties when using .each in jQuery with a JSON object?

I am trying to populate the values of <inputs> on a webpage using data from a JSON object. http://codepen.io/jimmykup/pen/exAip?editors=101 To begin, I create a JSON object with two values - one for name and one for url. var jsonObj = []; var nam ...

Extracting JavaScript variable data to PHP using Ajax technology

I've been trying to save latitude and longitude values in PHP variables, but I'm having trouble. Here is the code I'm using. Can anyone please help me figure out why these values are not being stored in PHP variables: Code: <!DOCTYPE h ...

What is the process to assign a value received from the server to an Input field and then update

I am working on an input field that should initially display a value from the server const [nameValue, setNameValue] = useState(""); <TextField id="outlined-read-only-input" label="Display Nam ...

Filtering doctrine queries by related entities within the same table

I am currently implementing API platform into my Symfony project and facing an issue. Within my project, I have 3 tables: account, user, and account_user. An account can be linked to a parent account. Now, when making a GET request to /accounts, I need t ...

Unable to access application in Laravel 7 Docker deployment

Two months ago, my two dockerized Laravel apps were functioning properly. However, now when I attempt to log in, the app fails to authenticate. Interestingly, unauthenticated routes work without any issues. The database is operational, and even migrations ...

Make the div disappear upon clicking the back button in the browser

When a user selects a thumbnail, it triggers the opening of a div that expands to cover the entire screen. Simultaneously, both the title and URL of the document are modified. $('.view-overlay').show(); $('html,body').css("overflow","h ...

Mastering the art of crafting effective regular expressions

I am still learning about this function, but I believe it is the perfect solution for validating a specific field. How can I restrict input in a textarea to only include the following characters with spaces or carriage returns: Allowed characters: number ...

Struggling with Implementing jQuery for Triggering Multiple Functions on a Single Change Event?

I am currently developing jQuery functions that create dependencies between the css classes of different inputs in a web form. For example, when a specific input has a certain value, the "hide" class is removed from another input. One working example of t ...

Leveraging thousands of on() event listeners in Firebase demonstrates exceptional design

We are looking to perform operations on our Firebase database and modify data based on user input from their mobile devices by changing a flag. Currently, we are using the `on()` method to listen to specific flags within each user's node. This listen ...

Avoid including package-lock.json file in GitHub contribution history

After the release of npm v5.0.0, utilizing npm packages automatically generates a package-lock.json file when running npm install. In my situation, my package-lock.json document is almost 10,000 lines long. Npm advises that this file should be committed: ...

While working with ajax form submission in CodeIgniter, I encountered an issue where I did not receive the expected value in the

For my project, I am using PHP Codeigniter and submitting a form through an Ajax call. However, I am facing an issue where I am not receiving the expected value in the success message. Below is the code snippet: Ajax Call: function addLocation(){ va ...

Transform an HTML table string into JSON format

I discovered a useful library called table to JSON that allows me to convert an HTML Table to JSON using its ID (#testtable in the example below): var table = $('#testtable').tableToJSON(); alert(JSON.stringify(table)); However, I am interested ...

Tips for showcasing elements individually in JavaScript when a button is clicked and halting on a random element

I have some names stored in h3 tags. I want to highlight one name at a time when I click a button, stopping at a random name. <div class="all-names"> <h3 class="name-one"><span class="line">Name ...

What is the reason for storing CRUD images in a temporary file?

When working on my Laravel application, I encountered an issue where the photo of a new book was being sent to a temporary file instead of the specified destination path. What could be causing this? Here are my functions: public function store(Request $re ...

Tips for specifying the "url" parameter in xmlhttp.open() for utilizing Ajax in communication with node.js

server.js has the ability to generate a random number. I am trying to retrieve a random number from the server and utilize xmlhttp to send a request. However, the string value remains unchanged when I visit http://localhost:3000/index.html. What could be c ...