PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, September 10, 2022

[FIXED] How to use json api specs in ajax?

 September 10, 2022     ajax, json-api     No comments   

Issue

How should my data be and how can I use JSON API specification with ajax to fetch data from an endpoint. Thanks in advance

is the format of my data correct?

photo link:https://drive.google.com/file/d/1j6At0x3kX3wAQ178IBVG4tOHOMUrpxcw/view?usp=drivesdk


Solution

The format of the data as shown in the screenshot included in your comment is not a valid JSON:API document. A JSON:API document must follow a specific schema as described in the JSON:API specification.

For example the JSON shown in your screenshot validates this requirement of the specification:

A document MUST contain at least one of the following top-level members:

  • data: the document’s “primary data”
  • errors: an array of error objects
  • meta: a meta object that contains non-standard meta-information.

https://jsonapi.org/format/#document-top-level

Also each object representing a resource violates several requirements of the JSON:API specification, for example:

A resource object MUST contain at least the following top-level members:

  • id
  • type

[...]

In addition, a resource object MAY contain any of these top-level members:

  • attributes: an attributes object representing some of the resource’s data.
  • relationships: a relationships object describing relationships between the resource and other JSON:API resources.
  • links: a links object containing links related to the resource.
  • meta: a meta object containing non-standard meta-information about a resource that can not be represented as an attribute or relationship.

https://jsonapi.org/format/#document-resource-objects

The JSON:API specification contains several examples of well-formed JSON:API documents. I would recommend to have a look at them to understand the specification better.



Answered By - jelhan
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing