Looking for recommendations on JSON format for an AJAX response?

Suppose I submit a form through Ajax and am awaiting a response from the server:

  • Pass/fail status
  • If fails, a compilation of validation errors including corresponding field ids/names, etc

Is there a widely adopted or recommended JSON format for organizing such information? If there is, I'd prefer to follow that rather than inventing my own convention.

Answer №1

One company that stands out for me is OmniTI, they have a great standard that I highly recommend:

{
    status : "success",
    data : {
        "posts" : [
            { "id" : 1, "title" : "A blog post", "body" : "Some valuable information" },
            { "id" : 2, "title" : "Another blog post", "body" : "Additional content" },
        ]
     }
}

In my work, I often utilize a modified version:

{
    status : "error",
    messages : {
        "some_field" : "message"
    }
}

Answer №2

Check out the proposal format from Peter Bui:

{
  status: "ok|redirect|error",
  to: "http://www.redirect-url.com",
  html: "<b>Insert html</b>",
  message: "Insert some message here"
}

Answer №3

{
    "status": "failed", 
    "items":
        [
             {"id": "item1", "name": "item1"},
             {"id": "item2", "name": "item2"},
             {"id": "item3", "name": "item3"}
        ]
}

Answer №4

Interesting. While there may not be a set standard, one approach could be as follows:

{
    "status": "failed",
    "issues":
        [
             {"code": "1234", "message": "invalid address format"},
             {"code": "5678", "message": "missing username"}
        ]
}

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

Implementing AngularJS table filters on user click

As a newcomer to angularjs, I am attempting to implement a filter on click. The user will select a source and destination, then click on the filter button. The table should display results based on the input. Upon page load, the table should already contai ...

Tips for delaying a node api's execution until a database query no longer returns null

While it may seem like an unnecessary complication, in my current scenario, it is exactly what I require. I am dealing with an API (API-1) that communicates with a third-party service. Instead of directly providing me with a response that I can send back ...

sequentially animating elements using animate css in a choreographed manner

I have created a unique jsfiddle with 20 boxes that I am trying to animate using the Animate.css plugin. The plugin can be found at daneden.me/animate. My goal is to animate each box one after the other in a sequential manner, but I seem to be having trou ...

Utilize TypeScript functions within Angular applications

I have successfully created a TypeScript module and after compiling it, I am generating a JavaScript file. However, when I try to use this JavaScript file within my Angular project in the index.html, it loads but I cannot access its functionality from any ...

Optimal method for retrieving data from asynchronous functions in JavaScript

Currently, I am using the twit library for nodejs which has async calls. In my code, I have created functions like the following: function getUserFromSearch(phrase) { T.get('search/tweets', { q: phrase+' lang:pt', count: 1 }, funct ...

Combine several items to form a single entity

When I have two objects returned from the database, my goal is to combine them into one object. Here are the examples: { "id": 4, "first_name": "s", "last_name": "a", ...

The dropdown menu vanishes from sight as soon as the cursor moves away from a link

Recently, I encountered an issue while trying to create a dropdown menu using Jquery. The problem arose when attempting to select the second link, as the entire menu would disappear. Additionally, is there a way to ensure that only one dropdown menu is vis ...

What approach provides the most effective way to navigate through an NPM Package?

I have an idea to enhance a particular open source NPM package. Although I understand the necessary steps, it would be incredibly beneficial to have the ability to debug the package while it is in operation. Is this achievable? ...

Trouble sliding with Material UI Slider

I've encountered an issue with my Material UI slider - it doesn't slide when clicked and dragged. I followed one of the examples on the Material UI website (https://material-ui.com/components/slider/) and included an onChange function. The values ...

Generate a unique slug in Javascript using the provided name and then show it in a disabled input field

Currently, I am working on a feature to generate a slug dynamically using Javascript. I want my users to be able to preview the slug before submitting the form. Below is the Javascript code I have written: function createSlug(text) { return text .toS ...

Invoke the loaded function using ajax

I am populating my div with data from an HTML file using the following code: $('#result').load('ajax/test.html'); Within test.html, there is a function called alertme. Is there a way to execute this function after the HTML is loaded i ...

Convert JSON data into HTML format

Can you assist me in creating the HTML DIV block for the response I received in JSON format? Here is the JSON format: [{"REL_NAME" : " 999999999","SO" : "","U_ID" : "105"}] Snippet: function ProcessData(data) { $('#accNo12').empty(); if (d ...

What's the Purpose of Using an Arrow Function Instead of Directly Returning a Value in a React Event Handler?

After skimming through various textbooks and blog posts, I've noticed that many explanations on event handlers in React are quite vague... For example, when instructed to write, onChange = {() => setValue(someValue)} onChange = {() => this.pro ...

How to save an image to a file using PHP and renaming the image file

To modify the default image name to imgName, it is necessary to send the parameter imgName along with the picture to PHP. var imgName = "30"; file_data = $('#pictureInput').prop('files')[0]; form_data = new FormDat ...

Can someone assist me in creating a clickable link that opens a menu in HTML when clicked?

I have been attempting for the past few days to open the megamenu by clicking on a link, but despite my efforts, I have not been successful. After reviewing some code, I discovered a clue in the CSS. It seems that setting the visibility value to visible wi ...

What is the best way to manage error responses using Retrofit 2?

Is there a way to effectively handle error responses in Retrofit 2 when using synchronous requests? In my scenario, I need to be able to process responses that typically return an array of pets under normal circumstances, but also handle cases where the r ...

Revamping an npm package on GitHub

Currently, I am managing a project that has gained popularity among users and has received contributions from multiple individuals. The next step I want to take is to convert the entire library into TypeScript, but I am unsure of the best approach to ach ...

Encountering a runtime exception when using Immutables with Retrofit after receiving an error from the server

When the server returns a 101 error, a runtime exception is thrown stating "Failed to invoke public ... TestResponse() with no args". The root cause of this issue is the failure to deserialize the JSON. How can I handle an error object instead of the "Test ...

What is the best way to add attachments to the clipboard in a Chrome extension?

One possible way to achieve this is by using the navigator.clipboard.write API, but keep in mind that this API is not available to background pages of Chrome extensions. A method I attempted involved creating a blob like this: let blobFinal = null; // ...

Transform the Json file into GraphJSON format for easy importing into Titan database

In my search for a solution to convert JSON files into GraphJSON graphs, I stumbled upon the GraphJSON Reader and Writer Library. One thing that confuses me is whether I can directly read from the path where a JSON file is stored and transform it into a g ...