如何在ajax json请求中调用url

how to call url in ajax json request

本文关键字:调用 url 请求 json ajax      更新时间:2023-09-26

我需要从项目中的一个目录调用webservice,但

url:"~/RA/WebServiceRAOpen.asmx/OpenedRAlistByBranch",但它不工作

$.ajax({
     url: "~/RA/WebServiceRAOpen.asmx/OpenedRAlistByBranch",
                            data: "{ 'ranumber': '" + request.term + "' }",
                            dataType: "json",
                            type: "POST",
                            contentType: "application/json; charset=utf-8",
    })

我可以从目录调用url吗

尝试使用上面的服务器路径构建url,例如使用window.location,如下所示:

$.ajax({
   url: window.location.host + "/RA/WebServiceRAOpen.asmx/OpenedRAlistByBranch"
...

重新绑定的url也应该起作用。去掉前缀"~"即可。请注意,您不应该在本地执行JavaScript代码,而应该从真正的http服务器执行。Visual Studio中的调试使用本地http服务器,因此可以这样做。

~/开头的URL是ASP。NET URL。要在JavaScript中使用它,您需要将它映射到实际的URL。将页面上的路径渲染为JavaScript变量,然后在脚本中使用它。

HttpContext.Current.Server.MapPath("~/RA/WebServiceRAOpen.asmx/OpenedRAlistByBranch")将为您提供实际路径。我没有使用ASP。NET,所以我再也不记得正确的asp:label语法了。