Javascript 在java脚本中从JSON加载内容之前返回值
我在java脚本中有一个函数,我使用它从我的asp.net mvc控制器获取JSON,在我的视图中显示该项:Javascript 在java脚本中从JSON加载内容之前返回值,javascript,jquery,ajax,asp.net-mvc,json,Javascript,Jquery,Ajax,Asp.net Mvc,Json,我在java脚本中有一个函数,我使用它从我的asp.net mvc控制器获取JSON,在我的视图中显示该项: <script type="text/javascript" language="javascript"> var k = 0; var record_count = 0; var col_count = 3; var row_count = record_count / col_count; var str = "<table>"; function item
<script type="text/javascript" language="javascript">
var k = 0;
var record_count = 0;
var col_count = 3;
var row_count = record_count / col_count;
var str = "<table>";
function itemTemplate() {
var url = '<%: Url.Content("~/") %>' + "ProductListing/AllProductListing/0";
$.getJSON(url, function (product) {
$.each(product.ja, function (index, value) {
//append rows and column to my table by concat the string of 'str'
});
});
str += '</table>';
alert(str);
return (str);
}
$(document).ready(function () {
alert(itemTemplate());
});
</script>
var k=0;
var记录_计数=0;
var col_计数=3;
var行计数=记录计数/列计数;
var str=“”;
函数itemTemplate(){
变量url=“”+“ProductListing/AllProductListing/0”;
$.getJSON(url、函数(产品){
$.each(product.ja,函数(索引,值){
//通过concat字符串'str'将行和列追加到我的表中
});
});
str+='';
警报(str);
返回(str);
}
$(文档).ready(函数(){
警报(itemTemplate());
});
问题:当我在$(document).ready
函数中警告函数时,首先它是警告
,然后继续警告我在$.getJSON
函数中连接它的完整字符串。因此,函数在获取JSON之前返回
有人知道吗?
谢谢。尝试将
async
设置为false,然后进行$.getJSON
调用
代码:
jQuery.ajax({async : false});
$.getJSON( ... );
参考:
注意:从jQuery 1.8开始,不推荐使用async:false。
替代解决方案
<script type="text/javascript" language="javascript">
var k = 0;
var record_count = 0;
var col_count = 3;
var row_count = record_count / col_count;
//var str = "<table>";
function itemTemplate(callback) {
var url = '<%: Url.Content("~/") %>' + "ProductListing/AllProductListing/0";
$.getJSON(url, callback);
//str += '</table>';
//alert(str);
//return (str);
}
$(document).ready(function () {
itemTemplate(function (product) {
var str = "<table>";
$.each(product.ja, function (index, value) {
//append rows and column to my table by concat the string of 'str'
});
str += "</table>";
alert(str);
});
});
</script>
var k=0;
var记录_计数=0;
var col_计数=3;
var行计数=记录计数/列计数;
//var str=“”;
函数itemTemplate(回调){
变量url=“”+“ProductListing/AllProductListing/0”;
$.getJSON(url,回调);
//str+='';
//警报(str);
//返回(str);
}
$(文档).ready(函数(){
itemTemplate(功能(产品){
var str=“”;
$.each(product.ja,函数(索引,值){
//通过concat字符串'str'将行和列追加到我的表中
});
str+=”;
警报(str);
});
});
尝试将async
设置为false,然后调用$.getJSON
代码:
jQuery.ajax({async : false});
$.getJSON( ... );
参考:
注意:从jQuery 1.8开始,不推荐使用async:false。
替代解决方案
<script type="text/javascript" language="javascript">
var k = 0;
var record_count = 0;
var col_count = 3;
var row_count = record_count / col_count;
//var str = "<table>";
function itemTemplate(callback) {
var url = '<%: Url.Content("~/") %>' + "ProductListing/AllProductListing/0";
$.getJSON(url, callback);
//str += '</table>';
//alert(str);
//return (str);
}
$(document).ready(function () {
itemTemplate(function (product) {
var str = "<table>";
$.each(product.ja, function (index, value) {
//append rows and column to my table by concat the string of 'str'
});
str += "</table>";
alert(str);
});
});
</script>
var k=0;
var记录_计数=0;
var col_计数=3;
var行计数=记录计数/列计数;
//var str=“”;
函数itemTemplate(回调){
变量url=“”+“ProductListing/AllProductListing/0”;
$.getJSON(url,回调);
//str+='';
//警报(str);
//返回(str);
}
$(文档).ready(函数(){
itemTemplate(功能(产品){
var str=“”;
$.each(product.ja,函数(索引,值){
//通过concat字符串'str'将行和列追加到我的表中
});
str+=”;
警报(str);
});
});
好吧,ajax就是这样工作的,$.getJSON
启动对服务器的异步调用,因此在ajax调用完成之前,您将点击下一行。您应该为getJSON
function itemTemplate() {
var url = '<%: Url.Content("~/") %>' + "ProductListing/AllProductListing/0";
$.getJSON(url, function (product) {
var html = "<table>";
$.each(product.ja, function (index, value) {
//append rows and column to my table by concat the string of 'str'
});
html += "</table>";
// now append html to the DOM
});
}
函数项模板(){
变量url=“”+“ProductListing/AllProductListing/0”;
$.getJSON(url、函数(产品){
var html=“”;
$.each(product.ja,函数(索引,值){
//通过concat字符串'str'将行和列追加到我的表中
});
html+=“”;
//现在将html附加到DOM中
});
}
函数将在您从服务器返回数据之前返回,因此您要对从服务器返回的数据执行的任何操作都必须在
.getJSON
的回调中。这就是ajax的工作方式,$.getJSON
启动对服务器的异步调用,因此,您将在ajax调用完成之前点击下一行。您应该为getJSON
function itemTemplate() {
var url = '<%: Url.Content("~/") %>' + "ProductListing/AllProductListing/0";
$.getJSON(url, function (product) {
var html = "<table>";
$.each(product.ja, function (index, value) {
//append rows and column to my table by concat the string of 'str'
});
html += "</table>";
// now append html to the DOM
});
}
函数项模板(){
变量url=“”+“ProductListing/AllProductListing/0”;
$.getJSON(url、函数(产品){
var html=“”;
$.each(product.ja,函数(索引,值){
//通过concat字符串'str'将行和列追加到我的表中
});
html+=“”;
//现在将html附加到DOM中
});
}
在从服务器获取数据之前,函数将返回,因此,您要对服务器返回的数据执行的任何操作都必须在
.getJSON
$的回调中。getJSON
异步执行,这意味着一旦发出请求,函数调用就被视为完成,JavaScript引擎将转到下一行代码:str+=''
function itemTemplate() {
var url = '<%: Url.Content("~/") %>' + "ProductListing/AllProductListing/0";
$.getJSON(url, function (product) {
var html = "<table>";
$.each(product.ja, function (index, value) {
//append rows and column to my table by concat the string of 'str'
});
html += "</table>";
// now append html to the DOM
});
}
您会收到两个警报,因为您在itemTemplate
函数中调用了alert
函数,该函数本身被调用以返回在$(document.ready
中进行的警报
函数调用的值。你的意思是要打两次警报吗
在任何情况下,就构建str
而言,您的意图似乎是在$完成后关闭表标记。每个都是如此
var k = 0,
record_count = 0,
col_count = 3,
row_count = record_count / col_count,
str = '<table>';
function itemTemplate() {
var url = '<%: Url.Content("~/") %>' + "ProductListing/AllProductListing/0";
$.getJSON(url, function (product) {
$.each(product.ja, function (index, value) {
//append rows and column to my table by concat the string of 'str'
});
str += '</table>';
alert(str);
});
}
$(document).ready(function () {
itemTemplate();
});
var k=0,
记录计数=0,
col_计数=3,
行计数=记录计数/列计数,
str='';
函数itemTemplate(){
变量url=“”+“ProductListing/AllProductListing/0”;
$.getJSON(url、函数(产品){
$.each(product.ja,函数(索引,值){
//通过concat字符串'str'将行和列追加到我的表中
});
str+='';
警报(str);
});
}
$(文档).ready(函数(){
itemTemplate();
});
$。getJSON
异步执行,这意味着一旦发出请求,函数调用即被视为完成,JavaScript引擎将转到下一行代码:str+='
您会收到两个警报,因为您在itemTemplate
函数中调用了alert
函数,该函数本身被调用以返回在$(document.ready
中进行的警报
函数调用的值。你是想打电话给alert
tw吗