Ways to restrict a JavaScript object from being sent through ajax requests

I have developed an application that utilizes JSON to send messages through ajax. Here is the JavaScript object used for this purpose:

var message = {
  "message_by": colmn[0].innerHTML,
  "message_date": new Date(),
  "message_recipients": [
    {
      "phone_number": colmn[1].innerHTML,
      "recipient_name": colmn[2].innerHTML
    }
   ],
  "message_text": colmn[3].innerHTML,
  "subscriber_name": "John Doe"
};

The sending process involves posting the message like this:

var url = "http://url/api/sendMessage";
   $.ajax({
           type: "POST",
           url: url,
           data: JSON.stringify(message),
           contentType: "application/json; charset=utf-8",
           dataType: "json",
           processData: true,
           success: function (data, status, jqXHR) {
               console.log(data);
               console.log(status);
               console.log(jqXHR);
               alert("Success. Message sent.");
           },
           error: function (xhr) {
               alert("Error. Try again.");
           }
   });

An example of a stringified message could be structured as follows:

var message = {
            "message_by": "Brian",
            "message_date": new Date(),
            "message_recipients": [{
                        "phone_number": "0700111222",
                        "recipient_name": "Timothy"
                 }, {
                     "phone_number": "0800222111",
                        "recipient_name": "Winnie"
                }],
            "message_text": "Hello! You are invited for a cocktail at our auditorium. ",
            "subscriber_name": "John Doe"
        }

However, I encountered an issue where messages with more than 100 recipients were failing to post to the API. Strangely, messages with up to 99 recipients worked fine. My colleague mentioned that there were no restrictions on the API's end.

Is there a way to limit the object size to 99 recipients and push the excess recipients to a new object while still maintaining them within the same ajax post request? Are there any creative solutions to overcome this limitation?

Answer №1

There is no specific limit set for message recipients.

If you wish to limit the message recipients to 99, you can achieve this by following the code snippet below:

validateMessage(message){
  var length = message.length;
  var messageRecipients = message.message_recipients.slice()
  for(var i = 0; i < length; i+=99){
    message.message_recipients = messageRecipients.slice(i, i+99)
    // Your post request goes here
  }
}

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

Easily accessible jQuery tabs with the option to directly link to a specific tab

I currently have tabs set up on my webpage. The code for these tabs is as follows: $('ul#tabs li a').click(function(){ var id = $(this).attr('id'); var counter = 1; $("ul#tabs a.current").removeClass('cur ...

Tips for extracting key/value pairs from a column containing JSON data in PostgreSQL

How can I extract values from a json column in my database? The values in this particular column are structured like the example below: column1 --------------------------------------------- "[{'name': 'Kate', 'po ...

Extract and loop through JSON data containing various headers

Having no issues iterating through a simple JSON loop, however, the API I am currently utilizing returns a response with multiple headers. Despite my efforts in various methods to access the results objects, I still face some challenges. The response struc ...

Backend error: Ajax and Node JS not passing parameter values correctly

While developing a web app, I encountered an issue when sending a post request with ajax to a node+express backend. The problem lies in the backend where all parameter values are showing as NULL. I have confirmed this by using console.log(data) on the fron ...

What steps can be taken to ensure the random generator continues to function multiple times?

My little generator can choose a random item from an array and show it as text in a div. However, it seems to only work once. I'm looking for a way to make it refresh the text every time you click on it. var items = Array(523,3452,334,31,5346); var r ...

`Adjusting function calls based on the specific situation`

On my webpage, I have implemented a tab system where clicking on a tab displays the corresponding content below. This functionality is controlled by a JavaScript/jQuery function called 'changeTab'. Now, I want to set up individual JavaScript fun ...

Assign the "this" keyword of App.vue to the Vuex state

I have discovered a workaround for accessing vue-router and other services that only works within a Vue component. By saving the "this" of the Vue app in the state of Vuex within the created option of App.vue. Is this a good approach or could it potential ...

PHP Ajax Login Form with Text File Authentication

I'm currently working on implementing an AJAX login form with PHP verification to check if the username and password exist in a text file. Here's the structure of the form: <form class="form login" id="log_user"> <input id="lo ...

How can I modify the appearance of a slider's range?

I'm struggling with customizing the style of a price slider input range. No matter what I do, the changes don't seem to take effect. Can anyone pinpoint where I may be going wrong? Appreciate any guidance on this! Thank you! <div class=" ...

Exploring a POST request using jQuery

Apologies if I struggle with my question or use incorrect terminology, as I am new to this. I have a basic web service that processes a JSON string and returns a JSON result. Here is the code: $jsonStr = ''; if(isset($_POST['request'] ...

Troubleshooting jQuery compatibility issues with Wordpress

Having some trouble implementing zclip on my Wordpress site to copy dynamically generated text. The code works fine as a standalone html page with embedded jquery, but it's not translating well to my Wordpress site. Even though I've placed the co ...

What is the best way to showcase data in a table using React Js?

I'm working on a project in React Js where I need to display data fetched from an API in a table. The challenge is that the data is received as an object and I'm struggling to map through it efficiently. Currently, I have hardcoded some sample da ...

The conversion from a !DOCTYPE of type java.lang.String to a JSONObject is not possible

I am in the process of developing an Android Studio application for user login and registration. As a beginner, I encountered a JSONException issue. The code I put together is based on this tutorial. Below is the description of the error: 06-12 05:17:31 ...

Guide to integrating and utilizing a personalized JavaScript file within TypeScript components in an Angular 2 application

I have created a standard Angular 2 App using angular-cli. Now, I am trying to incorporate a custom .js file into it. Here is a simplified version of what the file looks like: 'use strict'; var testingThing = testingThing || {}; testingThing. ...

Transmitting data from the front end to the server in React/Shopify for updating API information using a PUT request

After successfully retrieving my API data, I am now tasked with updating it. Within my component, I have the ability to log the data using the following code snippet. In my application, there is an input field where I can enter a new name for my product an ...

What is the best way to retrieve error messages from a transaction with ethers.js?

Incorporating a custom error in my Solidity contract: error Documents__AlreadyExist(string hash); The modifier I utilize is as follows: modifier alreadyExist(string memory hash) { if (s_documentsHashes[hash]) { revert Documents__AlreadyExi ...

Moving a DIV below a fixed-positioned element

I have a website with a scrollable div. It works well, but I also need an absolutely positioned div on top of it - and it still needs to scroll smoothly without any hindrance. You can check out a basic JSFiddle demonstration here: http://jsfiddle.net/41ra ...

Aurelia's numerous applications spread across various pages

Currently, I am in the process of finalizing the initial release of a large-scale website built using PHP (Phalcon), MySQL, and JQuery. The technology stack may be a bit outdated, but it was originally prototyped years ago and due to various circumstances, ...

Why does JavaScript function flawlessly in FireFox, yet fails completely in other web browsers?

When it comes to browsing, FireFox is my go-to browser, especially for testing out my website Avoru. However, I recently encountered an issue when checking the functionality of my code on other major browsers like Google Chrome, Opera, and Safari. It seems ...

Tips for executing a Python function from JavaScript, receiving input from an HTML text box

Currently, I am facing an issue with passing input from an HTML text box to a JavaScript variable. Once the input is stored in the JavaScript variable, it needs to be passed to a Python function for execution. Can someone provide assistance with this pro ...