AJax commit json and PHP receive json

Content

AJax

<script type="text/javascript">
var data = {
	id:"1";
	name:"test";
};
$.ajax({
                url:'/api.php?action=edit',
                type:'POST',
                contentType:'application/json;charset=utf-8',
                dataType:'json',
                // Data must be JSON formatted
                data: JSON.stringify(data),  
                success:function(result) {
                    // do something
                },
                error:function() {
                    // do something
                },

            });
</script>            

PHP

<?php 
$data= json_decode(file_get_contents('php://input'), true);
?>

Reference

https://imququ.com/post/four-ways-to-post-data-in-http.html
https://api.jquery.com/jquery.ajax/
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type
https://www.php.net/manual/en/wrappers.php.php#wrappers.php.input

发布了236 篇原创文章 · 获赞 145 · 访问量 44万+

猜你喜欢

转载自blog.csdn.net/u011944141/article/details/90765751