ANGULAR JS POST Data parsing

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.