Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8002873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:29:57+00:00 2026-06-04T16:29:57+00:00

I am using amplify.request , and I would like to have CRUD-like URLs when

  • 0

I am using amplify.request, and I would like to have CRUD-like URLs when sending data to and from the server. Here is an example:

Defining the resource

resources = {
"document_create"           : ['/d/crud/',           "POST"],
"document_read"             : ['/d/crud/{id}',       "GET"],
"document_update"           : ['/d/crud/{id}',       "PUT"],
"document_delete"           : ['/d/crud/{id}',       "DELETE"]
};

$.each(resources, function (resource, settings) {
  definition = {
    url     : settings[0],
    type    : settings[1],
    dataType: "json",  // what comes back
    decoder : 'jsend',
    contentType: 'application/json' // what goes there
  };

  amplify.request.define(resource, "ajax", definition);
});

Employing the resource

function make_request(resource, params, success_cb, error_cb) {
 if (this.is_post(resource)) {
   // this.is_post is a test, defined elsewhere, to see if this is a POST request
   params = JSON.stringify(params);
 }

 amplify.request(
   resourceId: resource
   data: params
   success: success_cb
   error: error_cb
 );
}

This works fine for create and read and `delete, like-so:

make_request('document_delete', {id: 1}, cb)

However, for update, since the content is being passed as JSON, the URL substitution does not occur as intended.

Is there a way to take advantage of the URL substitution for {id} in the above scheme?

The only alternative I can think of is to pass the data to the server URL-encoded. Unfortunately this is somewhat problematic, and I would prefer to be able to retain the use of CRUD-like URLs and saving with JSON formatted data, if that is possible.

Thoughts would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:29:58+00:00Added an answer on June 4, 2026 at 4:29 pm

    You could define your own request type, or you could just listen for request.ajax.preprocess and do your JSON.stringify there – which is after the URL substitution has occurred.

    The is_post is presumably the same code you have now, just put in a different place. It’s not a magic function 🙂

     amplify.subscribe( "request.ajax.preprocess", function( defnSettings, settings, ajaxSettings ) {
        if ( is_post( defnSettings.resourceId ) ) {
             // This will still include the variable that matches the URL substitution:
             var _settings = $.extend( true, {}, defnSettings.data, settings.data, ajaxSettings.data );
             ajaxSettings.data = JSON.stringify( _settings );
        }
     });
    

    Your make_request would no longer stringify:

    function make_request(resource, params, success_cb, error_cb) {
     amplify.request(
       resourceId: resource
       data: params
       success: success_cb
       error: error_cb
     );
    }
    

    Then your request can be run as normal:

    make_request('document_update', {id: 1, title: "New Title" }, cb)
    

    Important: the way I wrote the preprocess call, it will not remove the id even though it matched the URL (Amplify would normally delete the matched key). If you want it to remove the id from the stringified JSON, replace the _settings assignment with this:

    _settings = ajaxSettings.data;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When using amplify.request , I'd like to use amplify.store to persist the results from
Using Core Data, I have a fetch request to fetch the minimum of a
Using a restful resource in Rails, I would like to be able to insert
Using Flex 3, I would like to take an image snapshot such as this:
Using Rails 3.2.0 with haml and sass: I Would like to link an external
Using Jenkins or Hudson I would like to create a pipeline of builds with
Using Java,I have to fetch multiple sets of values from an XML file to
Using Android 2.1+. I have a service that gets killed from time to time
Using PHP, I can convert MySQL data or static table data to csv, Excel,
Using C# for ASP.NET and MOSS development, we often have to embed JavaScript into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.