Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
angularjs中的$http.post到达php,但angularjs中没有收到响应_Php_Angularjs - Fatal编程技术网

angularjs中的$http.post到达php,但angularjs中没有收到响应

angularjs中的$http.post到达php,但angularjs中没有收到响应,php,angularjs,Php,Angularjs,我有一个奇怪的问题,你已经做了几千次了,但这次不行。我已经用了两天了,没法修好 所以我的代码非常简单: js: test.php: <?php $user=json_decode(file_get_contents('php://input')); echo json_encode($user); ?> 但这给了我完全相同的行为 提前感谢您帮助我,祝我度过愉快的一天!;) PS:我正在使用PHPStorm,以防它与服务器重新启动有关。如果使用内容类型“application/x-w

我有一个奇怪的问题,你已经做了几千次了,但这次不行。我已经用了两天了,没法修好

所以我的代码非常简单:

js:

test.php:

<?php
$user=json_decode(file_get_contents('php://input'));
echo json_encode($user);
?>
但这给了我完全相同的行为

提前感谢您帮助我,祝我度过愉快的一天!;)


PS:我正在使用PHPStorm,以防它与服务器重新启动有关。

如果使用内容类型“application/x-www-form-urlencoded”,则应以urlencoded格式(即“var1=val1&var2=val2”)传递数据

否则,如果使用“application/json”,则可以直接传递javascript对象

如果我能更好地帮助你,请告诉我


第一次进近再见

要以正确的方式使用$http.post,您应该正确地使用.then方法,而不是success方法

$http.post('/someUrl', data, config).then(successCallback, errorCallback);
请检查是否使用$http.post

第二种方法

看起来数据转换不正确。默认情况下,$http服务将通过将数据序列化为JSON,然后使用内容类型“application/JSON”来转换传出请求。但是您希望将值作为表单post发布,因此需要更改序列化算法,并使用内容类型“application/x-www-FORM-urlencoded”发布数据

以下代码引用自

以及TransformRequestAsFormPost实现

app.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 );

}

}
);

我在这里遇到的问题来自我之前在JS中打开的文件阅读器:

reader.readAsBinaryString($scope.file);
    reader.onload = function (e) {
       //stuff
    };
readAsBinaryString函数使文件\u获取\u内容(“php://input)无法正常工作

一旦被替换为

    reader.readAsDataURL($scope.file);
一切都很顺利


感谢Giulio帮助我解决这个问题。

url应该是带域的完整url,或者以/和完整路径开头。。否则,请求可以放在某个内部文件夹中。@Svetlio调用php文件,从我测试的URL来看,这不是一个问题,以防php文件没有被调用,但它是。所以更改URL不会改变任何东西,或者会改变吗?那么为什么要使用php://input 而不是globals$\u POST?@Sveltio,因为默认情况下,$http.POST作为application/json运行,据我所知,您无法使用$\u POST从中获取数据。请尝试使用POST。。HTTP请求是HTTP请求,无论设置什么标题。是的,我在这里就是这么做的。出于测试目的,代码是“application/x-www-form-urlencoded”,但我使用的是带有javascript对象的“application/json”。但这是行不通的。我的php文件中有一次我的对象为空。请让我看看
print\r($\u请求)的结果好吗
Array()是我从print\r($\u REQUEST)得到的。太好了,那么你能在$http.post之前在javascript上做一个
console.log('data')
,让我看看你的数据内容吗?您可以在浏览器的开发工具中找到console窗口。正如我在文章中所说,当我在$http.post之前执行console.log(数据)时,我有一个包含两个字符串的对象,我无法发布结果,因为其中一个字符串非常长。我将对此进行测试。但是,我的第一个代码示例与假设不起作用吗?默认情况下,它是application/json,很好,我发送一个javascript对象,并使用$user=json\u decode(file\u get\u contents('php://input)去拿。你知道这个问题是从哪里来的吗?根据最新的angular文档,使用$http.post的正确方法是$http.post('/someUrl',data,config.),然后(successCallback,errorCallback);好吧,我错过了最新的更新。但这真的很奇怪:我在我的应用程序中到处都在这样做,它在任何地方都能工作,但在这里,我有完全相同的代码。我会尽快测试,然后告诉你情况如何。保持联系好的,这修复了“仅一次”错误,在测试我的应用程序后,$http.post根本不会调用。但对象仍然为空。如果http请求从浏览器触发,您是否可以在开发人员工具中进行检查?如果是,您从服务器获得的http响应状态是什么
app.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 );

}

}
);
reader.readAsBinaryString($scope.file);
    reader.onload = function (e) {
       //stuff
    };
    reader.readAsDataURL($scope.file);