score:1
Accepted answer
you can try as:
public function save()
{
return response()->json(['data' => 'here']);
}
the json method will automatically set the
content-type
header toapplication/json
, as well as convert the given array into json using the json_encode php function.
score:1
you are almost there. you must return another promise, that gets text or json from fetch:
fetch('http://laravel.dev/content', {
method: 'post',
headers: {
'accept': 'application/json',
'content-type': 'application/json',
},
body: json.stringify({
})
})
.then((response) => response.text()) //or response.json()
.then((text) => {
console.log(text);
});
also, you need to make a cors request, otherwise you can't access the response. you'll need to add this global middleware to laravel if you want to accept ajax requests from all origins
<?php
namespace app\http\middleware;
use closure;
class cors
{
/**
* handle an incoming request.
*
* @param \illuminate\http\request $request
* @param \closure $next
* @return mixed
*/
public function handle($request, closure $next)
{
return $next($request)
->header('access-control-allow-origin', '*')
->header('access-control-allow-methods', 'get,post,put,patch,delete,options')
->header('access-control-allow-headers', 'content-type');
}
}
read this article on global middleware.
Source: stackoverflow.com
Related Query
- Create a custom response from laravel using fetch API
- How to assign response from Api to an array using fetch function
- How to fetch data from Microsoft Custom Vision API using ReactJS
- CORS error using fetch API - React create app
- displaying data from fetch api using react
- Fetch data using an id from an api but display an alternative text to address bar e.g. content_title in Next.js
- Express does not receive parameters via POST from React using Fetch API
- I can't fetch data from the api using useEffect
- Fetch Foreign key object from an api using reactjs
- setState from an API response using superagent and React
- How can I create a button which Fetches image from API and download to the local with using ReactJs
- How to fetch data from API using id in reactJS?
- I can't get a response from github api using redux with react.js
- How to save CSV file from API response using React?
- How to set multiple states using one response from axios API call in React
- React js create a download file from a json response from API
- React Context API - create context from axios response
- Can't get response from ipify using fetch in React
- How to fetch data from a custom React hook (API) with onClick and display it in a Div using TypeScript?
- Fetch API using Saga didn't give any response
- How do I fetch search results from multiple API using reactjs
- Can I pass api response from a django views without using django-rest framework
- Render Cards after fetch data from api and create an object
- Please I'm using fetch to get data from an API, but only the JSX return without the products. Api is from: http://fakestoreapi.com/docs
- Is there a more efficient way to fetch data from an API using axios? (React.js & node.Js)
- How to create custom validation in a field that need value from another field using formik and yup
- How to fetch sequence api calls where second api call need particular data from first api call result using react hooks?
- How can you create a Tree display using dynamic data from an API in React?
- Posting data by combining the data from a form and a fetch api response
- React App: How to display data from api using fetch
More Query from same tag
- import Line from react-chartjs-2 from the wrong place
- Objects are not valid as a React child, render a collection of children, use an array instead
- Why Redux returns old state when dispatch action before?
- Reactjs Search after onChange
- Display both image and text from array of objects in React
- how to make material data grid width to fill the parent component in react js
- if-else is not working in scss with react
- Unhandled Runtime Error SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data in nextjs
- My Link tag from react-router doesn't change my page
- Onclick not displaying form react
- Type problems in reducer , typescript with useReducer
- UseEffect firing on initial render
- ReactJs, the SyntaxError with `static defaultProps`
- Filtering out an array depending on the value of object inside it
- To print values of an array in dropdown
- different way of calling a component
- Warning: Unknown prop on <> tag. Remove this prop from the element
- Children props are not visible in parent component
- Blurring background when React portal is displayed
- Login in a different Router -ReactJs-
- useHistory function in ReactJS
- ReactJS: How can I take a JSON entry from user and post into API?
- How to show tooltip only for show x values in react charts
- Uncaught Error: Cannot find module "./components/LoginPage"
- React-slick reinit
- In ReactJS, facing problem in rendering the data from an API due to its odd structure
- Eslint exhaustive deps warning in useEffect
- Deploying React app to Github User Page
- How to properly implement a JSONP form post submission on a Gatsby site
- Region is missing error - AWS Amplify React