Javascript Ajax Get方法未使用Ember Js重新调整Valiue
我试图用ember js显示下拉列表。使用AjaxGET方法。但它无法返回任何值 我的要求仅适用于Ajax 这是我的密码:Javascript Ajax Get方法未使用Ember Js重新调整Valiue,javascript,jquery,ajax,ember.js,handlebars.js,Javascript,Jquery,Ajax,Ember.js,Handlebars.js,我试图用ember js显示下拉列表。使用AjaxGET方法。但它无法返回任何值 我的要求仅适用于Ajax 这是我的密码: App.IndexController = Ember.ArrayController.extend({ model1: function () { var Category1 ; $.ajax({ url: 'http://localhost:63951/Home/GetAllCountry', type: 'GET',
App.IndexController = Ember.ArrayController.extend({
model1: function () {
var Category1 ;
$.ajax({
url: 'http://localhost:63951/Home/GetAllCountry',
type: 'GET',
datatype: 'application/json',
async: false,
success: function (data) {
alert(data);
Category1 = data;
}
});
return Category1;
}.property(),
});
注意
在我的控制器内,警报显示良好
警报Json是
[{“CountryID”:1,“CountryName”:“美国”}]
在我的html页面中,无法获取该值
js代码在这里
<script type="text/x-handlebars">
<h2>Welcome to Ember.js</h2>
{{outlet}}
</script>
<script type="text/x-handlebars" id="index">
<div>
{{view "select" id="AddProfile_SelectCountry" content=model1 prompt="Select Country" optionValuePath="content.CountryName" optionLabelPath="content.CountryName" valueBinding=default }}
</div>
</script>
<script src="js/libs/jquery-1.10.2.js"></script>
<script src="js/libs/handlebars-v1.3.0.js"></script>
<script src="js/libs/ember-1.8.1.js"></script>
<script src="js/appnew.js"></script>
<!-- to activate the test runner, add the "?test" query string parameter -->
欢迎来到Ember.js
{{outlet}}
{view“select”id=“AddProfile\u SelectCountry”content=model1 prompt=“select Country”optionValuePath=“content.CountryName”optionLabelPath=“content.CountryName”valueBinding=default}
您可以尝试:
$.ajax({
url: 'http://localhost:63951/Home/GetAllCountry',
type: 'GET',
datatype: 'application/json',
async: false,
success: function (data) {
App.IndexController = Ember.ArrayController.extend({
model1: data
})
}
});
而且:
{{ view Ember.Select
id="AddProfile_SelectCountry"
contentBinding="App.IndexController"
prompt="Select Country"
optionValuePath="model1.CountryName"
optionLabelPath="model1.CountryName"
valueBinding=default
}}
编辑:
这一个肯定会起作用,但不是真正的性能:
$.ajax({
url: 'http://localhost:63951/Home/GetAllCountry',
type: 'GET',
datatype: 'application/json',
async: false,
success: function (data1) {
$.ajax({
url: 'http://localhost:63951/Home/GetAllCities',
type: 'GET',
datatype: 'application/json',
async: false,
success: function (data2) {
App.IndexController = Ember.ArrayController.extend({
model1: data1
model2: data2
})
}
})
}
});
也许你可以试试这样:
App.IndexController = Ember.ArrayController.create({});
$.ajax({
url: 'http://localhost:63951/Home/GetAllCountry',
type: 'GET',
datatype: 'application/json',
async: false,
success: function (data) {
App.IndexController.extend({
model1: data
})
}
});
$.ajax({
url: 'http://localhost:63951/Home/GetAllCities',
type: 'GET',
datatype: 'application/json',
async: false,
success: function (data) {
App.IndexController.extend({
model2: data
})
}
});
但我不是余烬方面的专家,抱歉。当你做
分类1=数据代码>,您正在将变量标签类别1
设置为数据的值。您要做的是更改该标签引用的对象
最好是将Category1
设置为余烬对象,并根据数据设置其属性。大概是这样的:
model1: function () {
var Category1 = Ember.Object.create();
$.ajax({
url: 'http://localhost:63951/Home/GetAllCountry',
type: 'GET',
datatype: 'application/json',
async: false,
success: function (data) {
alert(data)
Category1.setProperties(data);
}
});
return Category1;
}.property(),
如果您的数据是以数组的形式输入的,但您只需要第一个元素(如上面所示),那么您应该执行Category1.setProperties(数据[0])代码>谢谢..我如何调用控制器url内的另一个ajax调用have Json。我已经提到了我的问题。该Json没有得到正确的输出。抱歉,我不明白你想告诉我什么。请你重新措辞好吗?我发明了localhost:63951/Home/GetAllCities
。将其替换为第二个AJAX请求的url。Category1返回Yes,它将是一个ember对象,因此控制台不会将其打印得很漂亮。但它将像普通JS对象一样可以访问数据。我已经更新了我的答案,如果数据是一个由一个项组成的数组,我会对其进行处理,如您的示例所示。嗨,carl。我已经替换了category1.setProperties(数据[0]);这一行。但我得到了相同的结果,只有未捕获的错误:断言失败:每个循环所覆盖的值必须是一个数组。你通过了@ArtjomB。我不相信这是重复的,因为OP似乎在Ember上遇到了问题,而不是JavaScript。@blong你说得对。不知道我在想什么。别担心@ArtjomB,我只是想为其他在这里游荡的可怜的灰烬人更新一下。