将AJAX $ get()方法为一个JavaScript变量 [英] Place ajax $get() into a javascript variable

查看:128
本文介绍了将AJAX $ get()方法为一个JavaScript变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要放置一个ajax的结果进入一个javascript变量。

I need to place the result of an ajax get into a javascript variable.

下面的作品

$.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);

我怎样才能得到它的工作?

How can I get it to work?

推荐答案

我从来没有使用 @ Url.Action 内的 $尝试。阿贾克斯调用(所以我不是100%肯定它的作品),但你可以尝试,因为它给你一个更细粒度的方法来Ajax请求使用它。在成功回调,您可以

I've never tried using @Url.Action inside an $.ajax call (so I'm not 100% sure it works), but you could try using it since it gives you a more granular approach to ajax requests. In the success callback, you could

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

$。阿贾克斯选项甚至接受异步命名的参数,你可以设置为每在您的评论虚假@ aroth的回答。

$.ajax options even accept a parameter named async which you can set to false per your comment in @aroth's answer.

这篇关于将AJAX $ get()方法为一个JavaScript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆