Home > Web Front-end > JS Tutorial > AngularJS uses the Post method to pass json parameters (code attached)

AngularJS uses the Post method to pass json parameters (code attached)

php中世界最好的语言
Release: 2018-04-27 17:26:17
Original
2904 people have browsed it

This time I bring you AngularJSThe idea of ​​using the Post method to pass json parameters (with code), AngularJS uses the Post method to pass json parametersWhat are the precautions, the following are Let’s take a look at practical cases.

This article mainly introduces how to use the Angularjs $http service to transfer json object data to the server using the POST method.

The details are as follows:

1. The default data type submitted by the $http POST method is application/json

var data = {'wid':'0', 'praise' : '25'}; 
$http.post(url, data).success(function(result) { 
 // 
});
Copy after login

The final request sent is:

POST http://www.example.com HTTP/1.1 
Content-Type: application/json;charset=utf-8 
 
{'wid':'0','praise':'25'}
Copy after login

The default method can directly pass the json object to the server in the form of string, which is more suitable for RESTful interfaces. But the $_POST of the php script cannot get the json data from the request body.

You can use this:

$data = file_get_contents("php://input"); //获得原始输入流
Copy after login

Note: php://input is invalid when enctype="multipart/form-data"

After obtaining the original input stream of the request and then processing it accordingly, you can get the json data.

2. Use x-www-form-urlencoded to submit and obtain json data

app.factory("Comment",function($http){
 return {
  get : function(commentFileUrl) {
   return $http({
    method: "GET",
    url: commentFileUrl,
    params: {R:Math.random()},
    headers: {'Cache-Control':'no-cache'}
   });
  },
  //保存一个评论
  save : function(toUrl,saveFileUrl,Data) {
   $http({
    method: "POST",
    url: toUrl,
    data: {saveUrl:saveFileUrl,commit:Data},
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    transformRequest: function(obj) {
     var str = [];
     for (var p in obj) {
      str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
     }
     return str.join("&");
    }
   }).success(function(data){
    console.log("数据已保存!");
   }).error(function(data) {
    alert("数据保存失败,错误信息:" + JSON.stringify({data:data}));
   });
  }
 }
});
Copy after login

I believe you have mastered the method after reading the case in this article , for more exciting content, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the use of vue.js tree control

Detailed explanation of the steps to implement file drag and drop in JS

The above is the detailed content of AngularJS uses the Post method to pass json parameters (code attached). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template