I'm new to programming staff and now trying Titanium mobile for IOS app with php and mysql.
我是编程人员的新手,现在正在使用php和mysql尝试使用Titanium mobile for IOS app。
problem is, when I got some array data from DB and try to pass to Titanium, "this.responseText" contains "null".
问题是,当我从DB获取一些数组数据并尝试传递给Titanium时,“this.responseText”包含“null”。
here is the part,
这是部分,
loadReq.onload = function()
{
var json = this.responseText;
var response = [];
response = JSON.parse(json);
Ti.API.info(response);
};
loadReq.onerror = function(event)
{
alert("Network error");
Ti.API.debug(event);
Ti.API.info(event);
};
loadReq.open("POST","http://localhost/myAppName/post_loading.php");
var params = {
userid: win.userid
};
loadReq.send(params);
and here is my php code.
这是我的PHP代码。
<?php
$con = mysql_connect('localhost','root','root');
if (!$con)
{
echo "Failed to connect.";
exit;
}
$db = mysql_select_db('myAppName');
if (!$db)
{
echo "Failed at selecting db.";
exit;
}
$userid = $_POST['userid'];
$sql = "here is sql order which will fetch array data based on $userid";
$query = mysql_query($sql);
$response = array();
if (mysql_num_rows($query) > 0)
{
$row = mysql_fetch_array($query);
$response = $row;
echo json_encode($response);
}
else
{
echo "there is no such data";
}
?>
array data that php file's getting from DB is like this,
php文件从DB获取的数组数据是这样的,
Array(
[0] => Array(
'id' => '1',
'name' => 'name1',
'sex' => 'm',
'age' => '20'
),
[1] => Array(
'id' => '3',
'name' => 'name3',
'sex' => 'f',
'age' => '25'
),
[2] => Array(
'id' => '5',
'name' => 'name5',
'sex' => 'm',
'age' => '18'
)
)
I tested some cases to make sure the HTTPClient is working, the sql order is correct in syntax, and single data (not multi-dimensional but just a array, value and words) is able to be passed properly.
我测试了一些情况以确保HTTPClient正常工作,sql顺序的语法正确,并且单个数据(不是多维的,只是数组,值和单词)能够正确传递。
However, multi-dimensional array is not available at the moment. Ti.API.info just telling me the response is "null"
但是,目前还没有多维数组。 Ti.API.info只是告诉我响应为“null”
any suggestion?
有什么建议吗?
thanks in advance.
提前致谢。
2 个解决方案
#1
0
Be sure to set your content-type before you echo the json. Add before the echo:
确保在回显json之前设置内容类型。在echo之前添加:
header('Content-type: application/json');
This sets a header in the http request so the javascript engine in titanium knows how to properly handle the response data
这在http请求中设置了一个标头,因此钛中的javascript引擎知道如何正确处理响应数据
#2
0
I finally got it. just need adding foreach function in php instead of only one echo.
我终于明白了。只需要在PHP中添加foreach函数而不是只有一个echo。
so the answer is;
所以答案是;
foreach($responses as $response)
{
echo $response;
}
such a simple mistake...took long enough :( hope this question save many others time.
这么简单的错误......花了足够长的时间:(希望这个问题可以节省很多其他时间。
#1
0
Be sure to set your content-type before you echo the json. Add before the echo:
确保在回显json之前设置内容类型。在echo之前添加:
header('Content-type: application/json');
This sets a header in the http request so the javascript engine in titanium knows how to properly handle the response data
这在http请求中设置了一个标头,因此钛中的javascript引擎知道如何正确处理响应数据
#2
0
I finally got it. just need adding foreach function in php instead of only one echo.
我终于明白了。只需要在PHP中添加foreach函数而不是只有一个echo。
so the answer is;
所以答案是;
foreach($responses as $response)
{
echo $response;
}
such a simple mistake...took long enough :( hope this question save many others time.
这么简单的错误......花了足够长的时间:(希望这个问题可以节省很多其他时间。