最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

快速解決angularJS中用post方法時(shí)后臺(tái)拿不到值的問題

 更新時(shí)間:2018年08月14日 08:38:53   作者:zsgod  
今天小編就為大家分享一篇快速解決angularJS中用post方法時(shí)后臺(tái)拿不到值的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

用angularJS中的$http服務(wù)碰到了一個(gè)問題:運(yùn)用$http.post方法向后臺(tái)傳遞數(shù)據(jù)時(shí),后臺(tái)的php頁面獲取不到data參數(shù)傳過來的值。

不論是這種姿勢(shì):

$http.post( "1.php", { id: 1 }).success(function (data) {
  console.log(data);
  });

還是這種姿勢(shì):

$http({
 method: 'POST',
 url: '1.php',
 data: { id: 1 }
 }).success(function (data) {
 console.log(data);
 });

后臺(tái)php中的$_POST或$_REQUEST都無法獲取到data中的值:

<?php
 echo json_encode($_POST);
?>

輸出為一個(gè)空數(shù)組。為了測(cè)試php本身是不是真的獲取不到值,我就寫了個(gè)表單測(cè)試下:

<form action="1.php" method="post">
 <input type="text" name="tid">
 <input type="submit" value="submit">
</form>

輸出結(jié)果為:{"tid":"2"},也就是說表單里的值是可以獲取的,但是用ajax發(fā)送的數(shù)據(jù)獲取不了!

那么表單數(shù)據(jù)和ajax發(fā)送的post數(shù)據(jù)之間有什么差異呢?于是我悄悄瞄一眼請(qǐng)求頭...

1.表單的請(qǐng)求頭部:

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.8,ja;q=0.6
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 5
Content-Type: application/x-www-form-urlencoded
Cookie: a0537_times=1
Host: 127.0.0.1
Origin: http://127.0.0.1
Pragma: no-cache
Referer: http://127.0.0.1/angularTest/1.html
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36

2.ajax發(fā)送的數(shù)據(jù)的請(qǐng)求頭部:

Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.8,ja;q=0.6
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 10
Content-Type: application/json;charset=UTF-8
Cookie: a0537_times=1
Host: 127.0.0.1
Origin: http://127.0.0.1
Pragma: no-cache
Referer: http://127.0.0.1/angularTest/1.html
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36

問題一下子就出來了!表單發(fā)送的文本類型是表單類型,而angular的ajax默認(rèn)發(fā)送的則是json數(shù)據(jù)。

那么怎么把Content-type給改了呢?于是我就打開了angular的官網(wǎng),照著改一下請(qǐng)求頭:

$http({
 method: 'POST',
 url: '1.php',
 data: { id : 1 }
 headers: {
  'Content-Type': 'application/x-www-form-urlencoded'
 }
 }).success(function (data) {
 console.log(data);
 });

于是輸出結(jié)果為:{"{\"test\":1}":""},還是有問題。對(duì)象并沒有自動(dòng)地序列化(jQuery用習(xí)慣了都快忘了居然還有這個(gè)問題?。?/p>

那么解決方案有:

1.不寫成對(duì)象的形式,直接寫字符串:

$http({
 method: 'POST',
 url: '1.php',
 data: 'test=1',
 headers: {
  'Content-Type': 'application/x-www-form-urlencoded'
 }
 }).success(function (data) {
 console.log(data);
 });

2.重寫angular中transformRequest,自己寫一個(gè)轉(zhuǎn)換方法:

 $http({
 method: 'POST',
 url: '1.php',
 data: $scope.data,
 headers: {
  'Content-Type': 'application/x-www-form-urlencoded'
 },
 transformRequest: function ( data ) {
  var str = '';
  for( var i in data ) {
  str += i + '=' + data[i] + '&';
  }
  return str.substring(0,str.length-1);
 }
 }).success(function (data) {
 console.log(data);
 });

3.重寫angular中的transformRequest,簡(jiǎn)單粗暴地把jquery拿過來:

 $http({
 method: 'POST',
 url: '1.php',
 data: $scope.data,
 headers: {
  'Content-Type': 'application/x-www-form-urlencoded'
 },
 transformRequest: function ( data ) {
  return $.param(data);
 }
 }).success(function (data) {
 console.log(data);
 });

4.修改默認(rèn)的transformations(這個(gè)不太熟,先看一眼官網(wǎng)上怎么說的):

Default Transformations

The $httpProvider provider and $http service expose defaults.transformRequest and defaults.transformResponse properties. If a request does not provide its own transformations then these will be applied.

You can augment or replace the default transformations by modifying these properties by adding to or replacing the array.

Angular provides the following default transformations:

Request transformations ($httpProvider.defaults.transformRequest and $http.defaults.transformRequest):

If the data property of the request configuration object contains an object, serialize it into JSON format.
Response transformations ($httpProvider.defaults.transformResponse and $http.defaults.transformResponse):

If XSRF prefix is detected, strip it (see Security Considerations section below).
If JSON response is detected, deserialize it using a JSON parser.

然后照抄:

app.config(['$httpProvider', function ( $httpProvider ) {
  $httpProvider.defaults.transformRequest = function ( data ) {
  var str = '';
  for( var i in data ) {
   str += i + '=' + data[i] + '&';
  }
  return str.substring(0,str.length-1);
  }
 }]);
<code class="language-javascript">$http({ 
 method: 'POST', 
 url: '1.php', 
 data: $scope.data, 
 headers: { 
  'Content-Type': 'application/x-www-form-urlencoded' 
 } 
 }).success(function (data) { 
 console.log(data); 
 });</code> 

以上這篇快速解決angularJS中用post方法時(shí)后臺(tái)拿不到值的問題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 如何通過簡(jiǎn)單的代碼描述Angular父組件、子組件傳值

    如何通過簡(jiǎn)單的代碼描述Angular父組件、子組件傳值

    Vue組件是學(xué)習(xí)Vue框架最比較難的部分,下面這篇文章主要給大家介紹了關(guān)于如何通過簡(jiǎn)單的代碼描述Angular父組件、子組件傳值的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • 詳解在Angular4中使用ng2-baidu-map的方法

    詳解在Angular4中使用ng2-baidu-map的方法

    這篇文章主要介紹了在Angular4中使用ng2-baidu-map的方法,本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-06-06
  • Angular開發(fā)者指南之入門介紹

    Angular開發(fā)者指南之入門介紹

    本篇文章主要介紹了Angular開發(fā)者指南的入門知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-03-03
  • 詳解Angular.js指令中scope類型的幾種特殊情況

    詳解Angular.js指令中scope類型的幾種特殊情況

    AngularJs最重要也是最難理解的模塊之一就是它的指令(directive)了,自定義指令配置有很多個(gè)參數(shù),下面這篇文章主要介紹了關(guān)于Angular.js指令中scope類型的幾種特殊情況,需要的朋友可以參考下。
    2017-02-02
  • 什么是 AngularJS?AngularJS簡(jiǎn)介

    什么是 AngularJS?AngularJS簡(jiǎn)介

    這篇文章主要介紹了什么是 AngularJS?AngularJS簡(jiǎn)介,本文講解了AngularJS方方面面的基礎(chǔ)知識(shí),AngularJS 是一個(gè)為動(dòng)態(tài)WEB應(yīng)用設(shè)計(jì)的結(jié)構(gòu)框架。它能讓你使用HTML作為模板語言,通過擴(kuò)展HTML的語法,讓你能更清楚、簡(jiǎn)潔地構(gòu)建你的應(yīng)用組件,需要的朋友可以參考下
    2014-12-12
  • AngularJS基礎(chǔ)知識(shí)

    AngularJS基礎(chǔ)知識(shí)

    這篇文章主要介紹了AngularJS基礎(chǔ)知識(shí),包括AngularJS定義和特點(diǎn)以及構(gòu)建AngularJS應(yīng)用的方法,推薦給大家。
    2014-12-12
  • angularJS結(jié)合canvas畫圖例子

    angularJS結(jié)合canvas畫圖例子

    這篇文章主要介紹了angularJS結(jié)合canvas畫圖例子的方法,需要的朋友可以參考下
    2015-02-02
  • angular學(xué)習(xí)之從零搭建一個(gè)angular4.0項(xiàng)目

    angular學(xué)習(xí)之從零搭建一個(gè)angular4.0項(xiàng)目

    本篇文章主要介紹了從零搭建一個(gè)angular4.0項(xiàng)目,主要用到的工具angular4.0、angular-cli、npm(v3.10.8)、node(v6.2.0),有興趣的可以了解一下
    2017-07-07
  • Angular.js實(shí)現(xiàn)掃碼槍掃碼并生成二維碼

    Angular.js實(shí)現(xiàn)掃碼槍掃碼并生成二維碼

    這篇文章主要為大家介紹了Angular.js實(shí)現(xiàn)掃碼槍掃碼并生成二維碼示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Angular依賴注入系統(tǒng)里Injection token PLATFORM_ID使用場(chǎng)景詳解

    Angular依賴注入系統(tǒng)里Injection token PLATFORM_ID使用場(chǎng)景詳解

    這篇文章主要為大家介紹了Angular依賴注入系統(tǒng)里Injection token PLATFORM_ID使用場(chǎng)景詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11

最新評(píng)論

顺昌县| 汉川市| 湖北省| 德令哈市| 尉犁县| 玉树县| 宁城县| 乌海市| 彭水| 通州区| 涡阳县| 香港 | 高密市| 四川省| 澄城县| 普定县| 城固县| 福安市| 阿瓦提县| 抚宁县| 元谋县| 鹤岗市| 克东县| 阿鲁科尔沁旗| 淮安市| 华坪县| 吉首市| 宣武区| 邵东县| 安达市| 绵竹市| 长葛市| 肇源县| 孝昌县| 亚东县| 长治市| 新建县| 克拉玛依市| 封开县| 海丰县| 德昌县|