[英]angularJs POST data in $_POST for php
我正在從AngularJS應用程序向PHP腳本發布請求。 我通過在網上查找答案來使它起作用,但是我想在$_POST['jwt']
變量的php腳本中獲取數據。
這是我的AngularJS代碼:
var dataReq = {
method: 'POST',
url: 'http://localhost/PHP/dataSender.php',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: { jwt: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9' }
}
$http(dataReq).success(function(response,data) {
console.log(response);
});
在我的PHP我有這個:
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
@$jwt = $request->jwt;
echo $jwt;
它正在工作,但是如果我將角度請求中的標題更改為
'Content-Type': 'application/json'
我收到CORS錯誤。
有沒有一種方法可以獲取$_POST
變量中的值,因為我從未使用過此file_get_contents("php://input");
如何檢查$_POST
是否設置了值,我將執行以下操作:
if(isset($_POST["jwt"]) && !empty($_POST["jwt"]))
{
echo $_POST["jwt"];
}
如何在此file_get_contents("php://input");
進行相同的測試file_get_contents("php://input");
使用轉換表單請求方法添加一個有角度的js工廠。
'use strict';
// I provide a request-transformation method that is used to prepare the outgoing
// request as a FORM post instead of a JSON packet.
angularApp.factory(
"transformRequestAsFormPost",
function() {
// I prepare the request data for the form post.
function transformRequest( data, getHeaders ) {
var headers = getHeaders();
headers[ "Content-type" ] = "application/x-www-form-urlencoded; charset=utf-8";
return( serializeData( data ) );
}
// Return the factory value.
return( transformRequest );
// ---
// PRVIATE METHODS.
// ---
// I serialize the given Object into a key-value pair string. This
// method expects an object and will default to the toString() method.
// --
// NOTE: This is an atered version of the jQuery.param() method which
// will serialize a data collection for Form posting.
// --
// https://github.com/jquery/jquery/blob/master/src/serialize.js#L45
function serializeData( data ) {
// If this is not an object, defer to native stringification.
if ( ! angular.isObject( data ) ) {
return( ( data == null ) ? "" : data.toString() );
}
var buffer = [];
// Serialize each key in the object.
for ( var name in data ) {
if ( ! data.hasOwnProperty( name ) ) {
continue;
}
var value = data[ name ];
buffer.push(
encodeURIComponent( name ) +
"=" +
encodeURIComponent( ( value == null ) ? "" : value )
);
}
// Serialize the buffer and clean it up for transportation.
var source = buffer
.join( "&" )
.replace( /%20/g, "+" )
;
return( source );
}
}
);
// -------------------------------------------------- //
// -------------------------------------------------- //
// I override the "expected" $sanitize service to simply allow the HTML to be
// output for the current demo.
// --
// NOTE: Do not use this version in production!! This is for development only.
angularApp.value(
"$sanitize",
function( html ) {
return( html );
}
);
添加表單保存控制器,並在控制器文件中使用轉換表單請求和標頭。
angularApp.controller('saveFormCtrl', function($scope, $http, transformRequestAsFormPost) {
$http({
transformRequest: transformRequestAsFormPost,
method : 'POST',
url : 'save.php',
data: {
jwt: $scope.jwt,
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
}
}).success(function(res){
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.