我有一个使用AngularJS和Onsen UI构建的工作的PhoneGap应用程序,该应用程序访问一个PHP文件,该文件在响应中获取一些数据。 php文件位于具有简单http的域中,但是现在我将其移至支持https的域中,我一直遇到一些奇怪的问题。
我尝试了AngularJS异步获取数据的方式以及ajax调用。
$http.get('https://blablabla.com/file.php?parameters').then(function(response) {
//SUCCESS
alert(response);
var z = response;
},function(response) {
alert("Failed");
});
它转到成功部分,但是我包含的数据不正确。它应该是一个字符串(一个普通的JSON字符串,我将其字符串化并转换为json对象)
ajax调用如下:
function httpGet(theUrl) {
var dataToReturn;
$.ajax({
type: 'GET',
url: theUrl,
cache: false,
async: false,
data: "",
success: function(data, textStatus, result) {
dataToReturn = data;
},
error: function (result, textStatus, errorThrown) {
alert("Failed to get response from server!");
}
});
return dataToReturn;
//var xmlHttp = new XMLHttpRequest();
//xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
//xmlHttp.send( null );
//return xmlHttp.responseText;
};
我基本上是从PhoneGap应用程序调用https中的php脚本。
如果我将地址更改为http,则可以正常使用...
我还想提醒您,PhoneGap基本上会创建某种本地代理。不过,我不确定如何详细说明...
关于如何解决此问题的任何想法,以获取与地址为简单“ http”时相同的数据?
最佳答案
Angular自动转换json,如果从PHP服务器端设置适当的标头,则效果会更好:
header('Content-Type: application/json');
在回声之前。
其次,您得到的内容似乎缺少括号
$http.get('https://blablabla.com/file.php?parameters')
.then(function(response) {
//SUCCESS
alert(response);
var z = response;
},function(response) {
alert("Failed");
});
第三,响应不再是直接响应,而是
response.data
所以你可能想尝试
alert(response.data);
var z = response.data;
警告!这取决于您的Angular.js版本
$ http对象有几种不同的语法,具体取决于您所使用的角度版本。