6

Visual Studio では、開発中のサイトに Javascript コードがいくつかあります。デバッグ中に、「localhost」への $ajax 呼び出しを使用しています。展開するときは、実際のサーバーである必要があります。

$('#textInput_UserName').focusout(function () {
    var _username = $('#textInput_UserName').val();
    $.ajax({
        url: 'http://localhost:8809/Account/UserNameExists/',
        data: { username: _username },
        dataType: 'html',
});

公開するときは、その localhost を実際のドメインに変換する必要があります。

$('#textInput_UserName').focusout(function () {
    var _username = $('#textInput_UserName').val();
    $.ajax({
        url: 'http://www.mydomain.com/Account/UserNameExists/',
        data: { username: _username },
        dataType: 'html',
});

Web Config 変換が機能する方法と同様に、これを行うための簡単で自動的な方法はありますか?

どうもありがとう!

4

3 に答える 3

3

ホストを省略するだけで、ブラウザが次のように入力します。

$('#textInput_UserName').focusout(function () {
    var _username = $('#textInput_UserName').val();
    $.ajax({
        url: '/Account/UserNameExists/',
        data: { username: _username },
        dataType: 'html',
});

あなたが実際に x-domain リクエストについて話しているのであれば (私はそうではないと思います)、グローバル js サイト変数を設定してください。

于 2012-06-06T15:15:53.257 に答える
1

これを使用することをお勧めします:

url: '<%= ResolveClientUrl("~/Account/UserNameExists/")',

このようにすると、次の場合に問題を回避できます。

  • ドメイン ルートではなく仮想ディレクトリにアプリをインストールする
  • ページをアプリ内の別のディレクトリ レベルに移動する
  • 異なるページでインスタンス化できるマスター ページまたはユーザー コントロールからサービスを使用するため、ディレクトリ レベル

ページ/ユーザー コントロール/マスター ページでパブリック プロパティを公開し、同じ方法でコードから使用することもできます。

  • ページ/uc/master のコード:public string ServiceUrl { get { return ResolveClientUrl("~/Account/UserNameExists/");}
  • .aspx のコード:url: '<%= ServiceUrl',
于 2012-06-07T23:51:22.877 に答える
0

Are you making a call to a web service or what is the destination of this url? When I am working with ajax calls in my web applications I usually set up the methods inside of a web service and call them like this:

 $.ajax({
        type: "POST",
        url: "../Services/BookingService.asmx/GetVerifiedReservations",
        data: paramsJson,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        success: function (response) {
            invalidDays = $.parseJSON(response.d);
        },
        error: function (xhr, textStatus, thrownError) {
            alert(textStatus);
            alert(thrownError);
        }
    });

As you can see the path is relative to the rest of the files in your domain.

于 2012-06-06T15:17:31.570 に答える