从当前URL中提取数据,并用它在Ajax调用作为paramenter它在、数据、URL、Ajax

2023-09-10 21:58:20 作者:不给糖就导弹!

我正在开发一个网站,其中只有1 HTML页面那里我第一次fethch的网址和放大器;会从URL中的ID和发送使用Ajax调用API。如果成功,从url.My code给定ID我显示的数据是AS-

I am developing a website in which only 1 html page is there in which I first fethch the url & gets the id from url and send it to api using ajax call. On success, I displays data of the given id from url.My code is as-

 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

    <script type="text/javascript">
        $(document).ready(function () {
            $('#main').hide();

            var url = window.location.href;
            var formno = url.substr(url.lastIndexOf('/') + 1);

            if (formno != 'Login.html') {              
                var apiUrl = 'http://localhost:801/api/api/Patient/Get';    
                $.ajax({
                    url: apiUrl,
                    crossDomain: true,
                    contentType: "application/json",
                    type: 'GET',
                    data: { formNo: formno },
                    success: function (result) {                           
                        $('#main').show();
                        alert("Success" + result)
                    },
                    error: function (result) {
                        alert("error :: " + JSON.stringify(result))
                    }
                });    
            }    
        });      
    </script>

在我使用的网址为 abc.in#1 它显示了成功的警报,但我想给的网址格式 abc.in / 1 在那个时候它给

when I use the url as abc.in#1 it displays the success alert but I want to give the url in format abc.in/1 at that time it gives

HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

为什么无法找到该网页?对此有任何解决方案? 我想给平淡的网址为 abc.in/1 其中1 ID 并且是动态的。 有没有什么解决办法吗?

Why it can not find the page? Is there any solution for this? I want to give plain url as abc.in/1 where 1 is id and which is dynamic. Is there any solution?

推荐答案

您的浏览器可能是试图访问文件的位置abc.in/1,它不存在。您将需要一些服务器端逻辑,对于这一点,如PHP的路由器,这将永远成为你的文件,并产生额外的参数会被它处理。 abc.in#1锚是不同类型的URL参数,其目的是通过文件或JavaScript在客户端进行处理的。

Your browser is probably trying to access document on location abc.in/1, which doesn't exist. You will need some server side logic for this, e.g. php router which will always serve your document, and additonal parameters will be processed by it. abc.in#1 anchor is different type of url parameter, which purpose is to be processed by document or javascript on client side.