ANGULAR JS POST data, the default is to send the JSON data to the server.
var params={defid: "1234"};
$http. Post (Url,params). Success (function (data, status, headers, config) {
});
We use Request.getparmeter ("Defid") when we obtain data from the server;
In this way we are not getting the data.
There are two ways to solve this problem:
1. Resolve on the service side
How to use the service side:
String json=fileutil.inputstream2string (Request.getinputstream ());
We parse this string by retrieving the data in the service-side using the stream, which will flow into a string.
2. In the browser-side resolution
Serializes the data into form submissions, using application/x-www-form-urlencoded encoding.
We write a generic conversion code.
var baseservices = angular.module ("Baseservices", []);
Baseservices.factory ("$jsonToFormData", function () {
function transformrequest (data, getheaders) {
var headers = getheaders ();
headers["Content-type"] = "application/x-www-form-urlencoded; Charset=utf-8 ";
return $.param (data);
}
return (transformrequest);
});
Write the Send request code as follows:
$http. Post (url,params,{transformrequest: $jsonToFormData}). Success (function (data, status, headers, config) {
});
In this way, the data is sent to the server using form encoding, and the server can use Reuqest.getparameter ("Defid") to get the data.
Attention:
var headers = getheaders ();
headers["Content-type"] = "application/x-www-form-urlencoded; Charset=utf-8 ";
This sentence, I wrote before is headers["Content-type", found a strange problem, is in Firefox can send data to the server.
Under Chrome, the same code is not available, the server received empty, after the browser capture packet analysis, found that there are two headers, one is Content-type,
One is Content-type, I changed the Content-type to Content-type overrides the default Application/json way, the data can be passed correctly.