简体   繁体   English

Jquery.ajax 更改 url 的 aspx 页面保存 web 方法后不起作用

[英]Jquery .ajax does not work after changing url of the aspx page holding webmethod

I'm new for Jquery.我是 Jquery 的新手。 I'm trying to call server method from my user control with Jquery ajax.我正在尝试使用 Jquery ajax 从我的用户控件调用服务器方法。 It is wired for me that when the server page that the request is sent to is located in root path, ajax works.对我来说,当请求发送到的服务器页面位于根路径中时,ajax 可以正常工作。 But when I moved the page into a subfolder and changed the url parameter of Jquery ajax, nothing happened...但是当我将页面移动到子文件夹并更改 Jquery ajax 的 url 参数时,什么也没发生...

Below is the web method in WebForm1.aspx.cs:下面是WebForm1.aspx.cs中的web方法:

namespace WebApplication11
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        [System.Web.Services.WebMethod]
        public static string test2()
        {
            return "TestString";
        }

       protected void Page_Load(object sender, EventArgs e)
       {

       }
   }

} }

When the page is located under root, it works.当页面位于根目录下时,它可以工作。 My ajax function in the user control is here:我在用户控件中的 ajax function 在这里:

<%@ Control Language="C#" AutoEventWireup="true" 
CodeBehind="WebUserControl1.ascx.cs"      
Inherits="WebApplication11.NewFolder1.WebUserControl1" %>

    <script type ="text/javascript">
    function ajaxTest()
    {

      $.ajax({
        type: "post",
        url: "WebForm1.aspx/test2",
        data: "{}",
        contentType: "Application/json; charset=utf-8",
        dataType: "json",
        success: function (r) {alert(r.d);} 


      })
    }
    </script>

    <input type = "checkbox" id = "c1" onchange = "ajaxTest()" />

But after I move the WebForm1.aspx page from root to a subfolder "NewFolder2", and changed the "url" in ajax method from "WebForm1.aspx/test2" to "/NewFolder2/WebForm1.aspx/test2" or "NewFolder2/WebForm1.aspx/test2", nothing happened.但是在我将 WebForm1.aspx 页面从根目录移动到子文件夹“NewFolder2”,并将 ajax 方法中的“url”从“WebForm1.aspx/test2”更改为“/NewFolder2/WebForm1.aspx/test2”或“NewFolder2/”之后WebForm1.aspx/test2",什么也没发生。

I am frustrated now.我现在很沮丧。 Can anybody give me a hint to figure it out?任何人都可以给我一个提示来弄清楚吗? Thank you very much!非常感谢!

Are you putting the path from the controls folder to the ajax method or from the page the control is put on?您是将控制文件夹的路径放置到 ajax 方法还是放置控件的页面? When a control is placed on a page, its ajax method calls must follow the path as if it was on the page itself because that is how it is rendered.当控件放置在页面上时,其 ajax 方法调用必须遵循路径,就好像它在页面本身上一样,因为这就是它的呈现方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM