MVC 4中的Ajax异步调用 [英] Ajax Async call in MVC 4

查看:101
本文介绍了MVC 4中的Ajax异步调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JS函数可以进行AJAX异步调用.

I have a JS function that makes an AJAX Async call.

function PostCall() {
        $('#txtRes').val('Please Wait......');
        var reqval = $('#txtReq').val();
        $.ajax({
            url: "@urlsMenu",
            type: 'post',
            data: "{'name':'" + reqval + "'}",
            async: true,
            contentType: "application/json",
             success: function (result) {
                debugger;
                $('#txtRes').val(result);

            }

        });
    }


我在Controller类中有一个函数可以进行Service调用.


I have a function in Controller class that makes a Service call.

public JsonResult PostAsync(string name)
        {
            string val = "Not Found ";
            MySr.Service1Client scc = new MySr.Service1Client();
           
          scc.GetDataCompleted += (result, e) =>
           {
               val = e.Result;
               string str = val;

           };
           scc.GetDataAsync(name);

           return Json(val, JsonRequestBehavior.AllowGet);
        }


但是,我的问题是,服务器端函数已经返回了值(如未找到"),而不是在webservice调用之后收到的值.

我希望函数在Web服务返回值后返回值.


But, my problem is, the Server-Side function already returns the value (as "Not Found") instead of the value received after the webservice call.

I want the function to return the value after the webservice returns the value.

推荐答案

(' #txtRes').val(' 请稍候.... ..'); var reqval =
('#txtRes').val('Please Wait......'); var reqval =


('
('#txtReq').val();


.ajax({ url:" , 类型:' post', 数据:" + reqval + " , 异步: true , contentType:" , 成功:功能(结果){ 调试器;
.ajax({ url: "@urlsMenu", type: 'post', data: "{'name':'" + reqval + "'}", async: true, contentType: "application/json", success: function (result) { debugger;


这篇关于MVC 4中的Ajax异步调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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