我需要在Web应用程序中将请求发送回我的服务器,例如,用户单击一个表项,然后它将额外的数据加载到同一页面的另一部分。</ p>

到目前为止,我一直通过AJAX请求执行此操作,这些请求运行良好(尽管如果这不是最佳实践,我会欢迎任何其他建议)。</ p>

我发现的问题是,当我将站点发布到IIS时,请求不再起作用。 仔细研究,我发现从IIS调用时URL缺少某些内容。</ p>

例如,在debug(localhost)中,我这样发出请求:</ p>

 等待$ .ajax({
     输入:“ GET”,
     网址:“ products?handler = productid =” + productId});
</ code> </ pre>

......其中'products'是我发出请求的页面的名称,其路径为 localhost / products </ code>。</ p>

从IIS托管时,URL现在为 localhost / mynewapp / products </ code>,因此基本路径已更改,这就是为什么我的请求无法在IIS中工作的原因。</ p>

我通过添加以下JS方法解决了此问题:</ p>

  function GetBaseUrl(){
     var url = window.location;

</ code> </ pre>

…,然后将AJAX调用更改为:</ p>

 等待$ .ajax({
     输入:“ GET”,
     网址:GetBaseUrl()+'?handler = productid ='+ productId});
</ code> </ pre>

这在调试和IIS中都有效。</ p>

这对我来说有点跷,所以我的问题是,应对这种情况的正确方法是什么?</ p>
     </ div>



I need to make requests back to my server in my web app, for example, the user clicks a table item and it loads extra data in another part of the same page.

So far I have been doing this via AJAX requests, which have been working fine (although if this is not the best practice way I would welcome any alternative suggestions).

The problem I found is that the requests no longer worked when I published the site to IIS. Looking into it I have found that the URL is missing something when called from IIS.

For example, in debug (localhost), I make my request like this:

await $.ajax({
    type: 'GET',
    url: 'products?handler=productid=' + productId});

...where 'products' is the name of the page I made the request from, for which the path is localhost/products.

When being hosted from IIS the url is now localhost/mynewapp/products, and so the base path has changed, which is why my requests don't work in IIS.

I've fixed this by adding the following JS method:

function GetBaseUrl() {
    var url = window.location;

    return url.pathname;

… and then changing the AJAX call to:

await $.ajax({
    type: 'GET',
    url: GetBaseUrl() + '?handler=productid=' + productId});

This works in both debug and IIS.

This just feels a bit hack-y to me, and so my question is, what is the proper way to deal with this situation?

Csdn user default icon