如何在用户控件中使用ajax方法 [英] How to use ajax method in user control

查看:84
本文介绍了如何在用户控件中使用ajax方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户控件。

其中,我使用的是Ajax post方法,但是当我在aspx页面中访问相同的用户控件时,它没有获取URL参数并给出我错误' undefined '。

我的代码如下:

I have one user control.
In that, I am using Ajax post method, but when I am accessing the same user control in my aspx page, it don't get URL parameter and gives me error 'undefined'.
My code is as below:

function GetRecords(index) {
    //alert(index);

    pageIndex = index;

    $("#loader").show();
    $.ajax({
        type: "POST",
        url: "Customer.ascx/GetCustomers",
        data: '{pageIndex: ' + pageIndex + '}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function (response) {
            alert(response.d);
        },
        error: function (response) {
            alert(response.d);
        }
    });
} 




[WebMethod]
public static string GetCustomers(int pageIndex)
{       
    return GetCustomersData(min, max, pageIndex).GetXml();
}

推荐答案

#loader)。show();
("#loader").show();


.ajax({
type: POST
url: Customer.ascx / GetCustomers
数据:' {pageIndex:' + pageIndex + ' }'
contentType: application / json; charset = utf-8
dataType: json
成功:OnSuccess,
失败:功能 (回复){
alert(response.d);
},
错误: function (response){
alert(response.d);
}
});
}
.ajax({ type: "POST", url: "Customer.ascx/GetCustomers", data: '{pageIndex: ' + pageIndex + '}', contentType: "application/json; charset=utf-8", dataType: "json", success: OnSuccess, failure: function (response) { alert(response.d); }, error: function (response) { alert(response.d); } }); }




[WebMethod]
public static string GetCustomers(int pageIndex)
{       
    return GetCustomersData(min, max, pageIndex).GetXml();
}






您正在制作的这个ajax电话,是它存在于ascx控件或一些单独的文件中。



我认识到的一件事是,你在这里给出的路径应该是完整的路径来自



根目录或使用'〜'运算符。因此路径没有问题。



路径的位置是它们被放置在控件中并引用此控件



在另一页。路径没有正确跟踪。



所以,你可以尝试两种方法中的一种(如果路径有问题)



使用:



1)var originalPath =< your_site_url_> +< webservices_directory>



现在你可以打电话给Url:originalPath +< webmethod_needed>





2)试试放一个'〜'操作符。并设置路径到webservices目录。



希望这会有所帮助。



你应该总是尝试在一个单独的文件中实现ajax调用,这样你就可以调试



并看到他们失败了。



快乐编码..
Hi,

This ajax call that you are making, is it existing on the ascx control or some separate file.

one thing i recognized is that, the path that you are giving here, should be complete path from

root directory or use a '~' operator. so that there will be no problem with the path.

The thing with the paths is when they were placed in the control and this control is referenced

in an other page. the path is not tracked properly.

so, you can try one of the two approaches (if the path is a problem)

use :

1) var originalPath="<your_site_url_>+ <webservices_directory>"

now you can call like" Url:originalPath+"<webmethod_needed>"


2) try to put a '~' operator. and set path to webservices directory.

Hope this helps.

you should always try to implement the ajax calls in a separate file so that you can debug

and see y they fail.

Happy coding..


这篇关于如何在用户控件中使用ajax方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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