gpt4 book ai didi

php - 使用 ajax json 动态响应设置 webos 列表选择器选项

转载 作者:行者123 更新时间:2023-11-29 00:56:15 29 4
gpt4 key购买 nike

我正在尝试开发一个应用程序,它使用列表选择器中的 ajax post 和更新从 MySQL 数据库获取响应,但是列表显示为空,请有人帮我解决这个问题......

.js 代码:

SecondAssistant.prototype.setup = function() {
this.selectorChanged = this.selectorChanged.bindEventListener(this);
Mojo.Event.listen(this.controller.get('firstselector'), Mojo.Event.propertyChange, this.selectorChanged);

this.names = [];

try {
new Ajax.Request('http://localhost/projects/testingasdf.php', {
method: 'post',
parameters: {
'recs': getallrecords,
'q': q
},
evalJSON: 'true',
onSuccess: function(response){
var json = response.responseJSON;
var count = json.count - 1;

for(i=0; i<count; i++){
this.names.push({
label: json[i].name,
value: '0'
});
}
this.controller.modelChanged(this.model);
}.bind(this),
onFailure: function(){
Mojo.Controller.errorDialog('Failed to get ajax response');

}

});

}
catch (e){
Mojo.Controller.errorDialog(e);
}

this.controller.setupWidget("firstselector",
this.attributes = {
label: $L('Name'),
modelProperty: 'currentName'
},
this.model = {
choices: this.names
}
);

};

php 代码:

<?php
header('Content-type: application/json'); // this is the magic that sets responseJSON


$conn = mysql_connect('localhost', 'root', '')// creating a connection



mysql_select_db("test", $conn) or die('could not select the database');//selecting database from connected database connection

switch($_POST['recs'])
{
case'getallRecords':{
$q = $_POST['q'];

//performing sql operations
$query = sprintf("SELECT * FROM user WHERE name= $q");
$result = mysql_query($query) or die('Query failed:' .mysql_error());
$all_recs = array();
while ($line = mysql_fetch_array($result,MYSQL_ASSOC)) {
$all_recs[] = $line;
}
break;
}
}

echo json_encode($all_recs);

// Free resultset
mysql_free_result($result);


// closing connection
mysql_close($conn);
?>

最佳答案

我会将模型更新代码移出 SecondAssistant.prototype.setup 方法,并让它在 SecondAssistant.prototoype.activate 中的某处触发。

也调用modelChanged

this.controller.modelChanged(this.model);

bindEventListener 上有一个拼写错误 - 应该是 bindAsEventListener 并且绑定(bind)的返回应该是一个不同的对象:

this.selectorChangedBind = this.selectorChanged.bindAsEventListener(this);

关于php - 使用 ajax json 动态响应设置 webos 列表选择器选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5979648/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com