Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 将ajax$get()放入javascript变量中_Asp.net Mvc 3_Jquery_Razor - Fatal编程技术网

Asp.net mvc 3 将ajax$get()放入javascript变量中

Asp.net mvc 3 将ajax$get()放入javascript变量中,asp.net-mvc-3,jquery,razor,Asp.net Mvc 3,Jquery,Razor,我需要将ajax get的结果放入javascript变量中 以下作品 $.get('@Url.Action("_Edit", "News", null)/' + guid_News, function (html) { $("#divEdit").html(html); }); 这行不通 var editHtml = ""; $.get('@Url.Action("_Edit", "News", null)/' + guid_News, function (html) { e

我需要将ajax get的结果放入javascript变量中

以下作品

$.get('@Url.Action("_Edit", "News", null)/' + guid_News, function (html)
{
    $("#divEdit").html(html);
});
这行不通

var editHtml = "";
$.get('@Url.Action("_Edit", "News", null)/' + guid_News, function (html)
{
    editHtml= html;
});
$("#divEdit").html(editHtml);
也尝试过

var editHtml = "";
editHtml = $.get('@Url.Action("_Edit", "News", null)/' + guid_News, function (html)
{
    return html;
});
$("#divEdit").html(editHtml);

我如何才能让它工作?

这不起作用的原因:

var editHtml = "";
$.get('@Url.Action("_Edit", "News", null)/' + guid_News, function (html)
{
    editHtml= html;
});
$("#divEdit").html(editHtml);
…因为这部分是:

它不会立即执行,也不会阻止随后语句的执行。它将在服务器返回其对请求的响应时执行,但到那时,您的
$(“#divEdit”).html(editHtml)语句已在
editHtml
设置为空字符串的情况下执行

这应该起作用:

var editHtml = "";
$.get('@Url.Action("_Edit", "News", null)/' + guid_News, function (html) {
    editHtml= html;
    setDivHtml();
});

function setDivHtml() {
    $("#divEdit").html(editHtml);
}

这不起作用的原因是:

var editHtml = "";
$.get('@Url.Action("_Edit", "News", null)/' + guid_News, function (html)
{
    editHtml= html;
});
$("#divEdit").html(editHtml);
…因为这部分是:

它不会立即执行,也不会阻止随后语句的执行。它将在服务器返回其对请求的响应时执行,但到那时,您的
$(“#divEdit”).html(editHtml)语句已在
editHtml
设置为空字符串的情况下执行

这应该起作用:

var editHtml = "";
$.get('@Url.Action("_Edit", "News", null)/' + guid_News, function (html) {
    editHtml= html;
    setDivHtml();
});

function setDivHtml() {
    $("#divEdit").html(editHtml);
}

我从未尝试过在
$.ajax
调用中使用
@Url.Action
(因此我不能100%确定它是否有效),但您可以尝试使用它,因为它为ajax请求提供了更细粒度的方法。在
success
回调中,您可以

$.ajax({
    url: '@Url.Action("_Edit", "News", null)/' + guid_News,
    type: 'GET',
    //async: false,
    success: function(data) {
        $('#divEdit').html(data);
    }
});

$.ajax
选项甚至接受一个名为
async
的参数,您可以根据@aroth的回答中的注释将该参数设置为false。

我从未尝试在
$.ajax
调用中使用
@Url.Action
(因此我不能100%确定它是否有效),但是您可以尝试使用它,因为它为您提供了一种更细粒度的ajax请求方法。在
success
回调中,您可以

$.ajax({
    url: '@Url.Action("_Edit", "News", null)/' + guid_News,
    type: 'GET',
    //async: false,
    success: function(data) {
        $('#divEdit').html(data);
    }
});

$.ajax
选项甚至接受名为
async
的参数,您可以根据@aroth的回答中的注释将该参数设置为false。

您知道如何使其同步并获取变量中的值吗?@Valamas-是的,请查看文档以了解。您只需将
.get()
替换为
.ajax()
,并将
async
选项设置为
false
。您知道如何使其同步并获取变量中的值吗?@Valamas-是的,请查看文档以了解。您只需将
.get()
替换为
.ajax()
,并将
async
选项设置为
false