Ajax函数不会在asp.net中使用Success或Error函数 [英] Ajax function is not going to either Success or Error functions in asp.net

查看:73
本文介绍了Ajax函数不会在asp.net中使用Success或Error函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,我对asp.net中的ajax函数有一点怀疑



我有一个ajax函数在Site1.Master的Page Load上调用我可以使用来自site1.master继承的所有页面中的函数的结果。



所以我把ajax函数放在单独的js文件中并包含我的site1.master中的文件在其他js文件加入之前的第一个位置。



当该site1.master加载时(很可能在用户登录后)我能够转到该功能,但我无法转到成功或错误功能。所以我添加了一个属性async:false。问题仍然存在。



请任何人都可以帮助我。



这是我的ajax代码在ap.js文件中



  var  AllItems =  new  Array(); 

函数GetAllItemsOnLoad(){
var aid = 1 ;
var param = ' {AgencyId :' + aid + ' }';
尝试 {
$ .ajax
({
type: POST
async false
contentType: application / json; charset = utf-8
url:WebSvcUrl + / GetAllItems
数据: param,
dataType: json
成功:OnSuccessGetAllItemsOnLoad,
error:OnFailOfGetAllItemsOnLoad
});
}
catch (错误)
{alert( < span class =code-string>无法获取数据,请再试一次); }
} // 获取所有项目

函数OnSuccessGetAllItemsOnLoad (msg){
尝试
{
if (msg。 d!= null
{
AllItems = msg.d;
}
其他
{
alert( 无法检索数据,请重试);
}
}
catch (错误)
{alert( 无法获取数据,请重试); }
} // on func succ get all items

function OnFailOfGetAllItemsOnLoad(request,status,error){
$( #lblErrIname)。 html( 无法获取数据,请再试一次);}

这里 我正在调用的代码 我的site1.Master页面

if (LoginUser == 3
{
ShowAdminDiv();
GetAllItemsOnLoad();
}


请问任何人可以告诉我其中我错了。

谢谢和问候
Ganesh

解决方案

.ajax
({
类型: POST
async false
contentType: application / json; charset = utf-8
url:WebSvcUrl + / GetAllItems
data:param,
dataType: json
成功:OnSuccessGetAllItemsOnLoad,
错误:OnFailOfGetAllItemsOnLoad
});
}
catch (错误)
{alert( < span class =code-string>无法获取数据,请再试一次); }
} // 获取所有项目

函数OnSuccessGetAllItemsOnLoad (msg){
尝试
{
if (msg。 d!= null
{
AllItems = msg.d;
}
其他
{
alert( 无法检索数据,请重试);
}
}
catch (错误)
{alert( 无法获取数据,请重试); }
} // on func succ get all items

function OnFailOfGetAllItemsOnLoad(request,status,error){


#lblErrIname )。html( 无法获取数据,请重试);}

这里 我正在调用的代码 我的site1.Master页面

if (LoginUser == 3
{
ShowAdminDiv();
GetAllItemsOnLoad();
}


请问任何人可以告诉我其中我错了。

谢谢和问候
Ganesh


您好,我错过了参数传递



成功:OnSuccessGetAllItemsOnLoad,
错误:OnFailOfGetAllItemsOnLoad





你可以试试这样的正确代码



成功:OnSuccessGetAllItemsOnLoad(响应),
错误:OnFailOfGetAllItemsOnLoad(msg )


Hi friends, I have small doubt in ajax function in asp.net

I have one ajax function which is called on Page Load of Site1.Master so that I can use that result out come from the function in all pages that inherit from site1.master.

So I have placed that ajax function in separate js file and included that file in my site1.master at first place before other js files to inculde.

When that site1.master loads (most probably after the user login) I am able to go to the function, but I am unable to go to either "Success" or "error" functions. so I have added a attribute "async:false". still the problem is persists.

Please any one can help me.

Here is my ajax code in ap.js file

var AllItems = new Array();

function GetAllItemsOnLoad() {
    var aid = 1;
    var param = '{"AgencyId":"' + aid + '"}';
    try {
       $.ajax
        ({
            type: "POST",
            async: false,
            contentType: "application/json; charset=utf-8",
            url: WebSvcUrl + "/GetAllItems",
            data: param,
            dataType: "json",
            success: OnSuccessGetAllItemsOnLoad,
            error: OnFailOfGetAllItemsOnLoad
        });
    }
    catch (err)
    { alert("Unable to get the data, Please Try again"); }
} // of Get All Items

function OnSuccessGetAllItemsOnLoad(msg) {
    try
    {
        if (msg.d != null)
        {
            AllItems = msg.d;
        }
        else
        {
             alert("Unable to retrive data, Please Try again");
        }
    }
    catch (err)
    { alert("Unable to get the data, Please Try again"); }
} // on func succ get all items

function OnFailOfGetAllItemsOnLoad(request, status, error) {
    $("#lblErrIname").html("Unable to get the data, Please Try again");}

Here is the code I am calling in my site1.Master page

  if (LoginUser == 3)
 {
            ShowAdminDiv();
            GetAllItemsOnLoad();
 }
 

Please any one Could tell me where I am wrong.

Thanks and Regards
Ganesh

解决方案

.ajax ({ type: "POST", async: false, contentType: "application/json; charset=utf-8", url: WebSvcUrl + "/GetAllItems", data: param, dataType: "json", success: OnSuccessGetAllItemsOnLoad, error: OnFailOfGetAllItemsOnLoad }); } catch (err) { alert("Unable to get the data, Please Try again"); } } // of Get All Items function OnSuccessGetAllItemsOnLoad(msg) { try { if (msg.d != null) { AllItems = msg.d; } else { alert("Unable to retrive data, Please Try again"); } } catch (err) { alert("Unable to get the data, Please Try again"); } } // on func succ get all items function OnFailOfGetAllItemsOnLoad(request, status, error) {


("#lblErrIname").html("Unable to get the data, Please Try again");} Here is the code I am calling in my site1.Master page if (LoginUser == 3) { ShowAdminDiv(); GetAllItemsOnLoad(); } Please any one Could tell me where I am wrong. Thanks and Regards Ganesh


Hello, my be you have missing the parameter passing

success: OnSuccessGetAllItemsOnLoad,
            error: OnFailOfGetAllItemsOnLoad



you can try the correct code like this

success: OnSuccessGetAllItemsOnLoad(response),
            error: OnFailOfGetAllItemsOnLoad(msg)


这篇关于Ajax函数不会在asp.net中使用Success或Error函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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