调用jquery ajax从外部js文件中使用WebMethod [英] calling jquery ajax to consume WebMethod from external js file

查看:59
本文介绍了调用jquery ajax从外部js文件中使用WebMethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$.ajax(
   {
       type: "POST",
       url: "Default.aspx/GetAge",
       data: "{}",
       contentType: "application/json; charset=uft-8",
       dataType: "json",
       success: function (rsp) { SetAge(rsp); },
       error: function (rsp)
       {
           alert(rsp); 
       }
   });

现在它报告以下错误:找不到资源".我发现自己错误地指定了网址.该javascript文件位于与Default.aspx位于同一目录中的外部文件中,有什么想法吗?

Right now it reports the following error: "Cannot locate the resource". I figure that I am specifying the url incorrectly. This javascript is in an external file located in the same directory as Default.aspx ... any thoughts?

推荐答案

我认为您的网址不正确-您使用的是相对网址,浏览器将它们视为相对于当前页面.它不被视为相对于js文件.例如,如果您正在尝试从页面上的www.xyz.com/SomeFolder/page1.aspx请求此请求,那么您的ajax请求将被定向到www.xyz.com/SomeFolder/Default.aspx/GetAge,因此请相应地调整url.

I believe that your URL is incorrect - you are using relative url and they are treated by browser as relative to current page. It is not considered relative to js file. For example, if your are trying this request from a page at say www.xyz.com/SomeFolder/page1.aspx then your ajax request would get directed to www.xyz.com/SomeFolder/Default.aspx/GetAge so adjust the url accordingly.

如果可能,请使用诸如/services/Default.aspx/GetPage之类的url-注意前导斜杠-无论您从哪个页面调用它们,它们都将始终被翻译为<your domain>/services/Default.aspx/GetPage.

If possible, use urls in form such as /services/Default.aspx/GetPage - note leading slash - they will be always get translated into <your domain>/services/Default.aspx/GetPage irrespective of page from which you are invoking them..

这篇关于调用jquery ajax从外部js文件中使用WebMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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