2017-04-25 40 views
0

萨拉姆aleykum,我想要在这里使用mappage路线调用一个AJAX服务器端方法,但它总是说代码C#:调用AJAX服务器端方法与mappage路线C#

[System.Web.Services.WebMethod] 
     public static bool RemovePhotofromAlbum(string list_photos_hotel) 
     { 
      ..... 
      return true; 
     } 

这里的jQuery代码想:

function RemovePhotofromAlbum(list_photos_hotel) { 
       $.ajax({ 
        type: "POST", 
        url: $(location).attr('pathname') + "/RemovePhotofromAlbum", 
        data: '{list_photos_room_type: "' + list_photos_hotel + '" }', 
        contentType: "application/json; charset=utf-8", 
        dataType: "json", 
        success: function (response) { 
         .... 
        }, 
        failure: function (response) { 
         alert(response.d); 
        } 
       }); 
      } 

不使用它的工作mappage路线。 但在这里我想要使用映射路线 我知道ajax方法中的URL存在问题,但我不知道如何解决它。 任何帮助,将不胜感激。 :)

如果这是不可能的只是告诉我

回答

0

$(location).attr('pathname')

使用这条线在DOM

附加属性,所以,如果你想保存的BaseURL,保持它在web.config,隐藏字段或任何其他js file而不是使用它

+0

没有,这里'$(位置).attr( '路径')'我试图访问方法示例的url:www.xxxx.com/mappagename并比方法名称:“/ RemovePhotofromAlbum” –

+0

首先在变量中创建完整路径,然后发送到ajax调用 – Tauqeer

+0

请举例说明 –

0

您应该使用$(位置).attr('href')而不是$(lo阳离子).attr( '路径')

,你必须与你的参数名称错误,应该是 'list_photos_hotel' 而不是 'list_photos_room_type'

试试这个:

function RemovePhotofromAlbum(list_photos_hotel) { 
    $.ajax({ 
     type: "POST", 
     url: $(location).attr('href') + "/RemovePhotofromAlbum", 
     data: '{list_photos_hotel: "' + list_photos_hotel + '" }', 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (response) { 

     }, 
     failure: function (response) { 
      alert(response.d); 
     } 
    }); 
} 

假设你运行您的服务器方法运行时来自相同aspx页面的脚本。

编辑:

因为你使用的地图路线,你会得到404你应该通过物理位置。

你的方法路径:管理/ admin_2 /的Index.aspx:

function RemovePhotofromAlbum(list_photos_hotel) { 
$.ajax({ 
    type: "POST", 
    url: "/Manage/admin_2/index.aspx/RemovePhotofromAlbum", 
    data: '{list_photos_hotel: "' + list_photos_hotel + '" }', 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function (response) { 

    }, 
    failure: function (response) { 
     alert(response.d); 
    } 
}); 

}

+0

我做了你所说的我用'href'替换'pathname'..同样的问题 –

+0

在ajax之前添加console.log($(location).attr('href'))打电话看看究竟是什么网址。 – KD2ND

+0

它给出了完整的路径,并没有解决我的问题 –

相关问题