2011-12-22 7 views
0

ローカルホストとプロダクションサーバーから作業するには、次のjQuery呼び出しが必要ですが、これを行う方法を理解できません。これはマスターページにあり、呼び出しはファイル構造内のさまざまな場所から来る可能性があります。私は確かにどんな助けにも感謝します。ASP.NETとjQuery:localhost/prodに関係なく、ファイルツリー内のどこからでもWebサービスを呼び出しますか?

 function getInternetLeadsCount() { 
      $.ajax({ 
       type: "POST", 
       url: "http://localhost:64558/mysite.com/Web_Services/AjaxWebService.asmx/HelloWorld", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       data: "{}", 
       success: AjaxSucceeded, 
       error: AjaxError 
      }); 
     } 

     function AjaxSucceeded(data, status) { 
      alert('jQuery: ' + data.d); 
     } 

     function AjaxError(x, y, z) { 
      alert(x + ' ' + y + ' ' + z); 
     } 

答えて

0

は、絶対URLを使用します。

URL: "/Web_Services/AjaxWebService.asmx/HelloWorld"

+2

という相対URLではないですから来た、働いた何ですか? –

+0

私は私の最初の試練の1つとしてそれを試みて、いつも「見つけられません」。 –

0
you can make webmethod in your aspx page and call that method like this. 

$.ajax({ 
    type: "POST", 
    url: "defaul.aspx/HelloWorld", 
    data: "{}", 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function(msg) { 
alert(msg.d); 
    } 
}); 

OR you can have the absoulte path 

$.ajax({ 
    type: "POST", 
    contentType: "application/json; charset=utf-8", 
    url: "WebService.asmx/HelloWorld", 
    data: "{}", 
    dataType: "json" 
}); 
+0

これは、マスターページにあるので、これを必要とする各ページにWebメソッドを作成する必要があります。 –

+0

あなたはすべてのページに$ .ajax関数を置くことができます。または、サーバー変数の名前にページ名を付けることができます。パブリックstring _Pagename {get; set;}これをマスターページに入れ、すべてのページに設定します。今のようにURLを作ることができます。 url: "'+ <%= _ページ名%> +'" –

0

まず第一に、私はあなたにIISまたはIIS Expressをインストールすることをお勧めしますがワークステーションではなく、組み込みのVisual Studioサーバーを使用しています。プロダクションサーバーと同じ環境で開発する必要があります。そうしないと、問題が発生する可能性があります。

とは別に、私はおそらく真のウェブルートを含むJavaScript変数をレンダリングするためにC#を使用します。あなたのマスターページまたはASPXファイルで:あなたのワークステーションで

<script type="text/javascript"> 
    var webRoot = '<%= Page.ResolveUrl("~/") %>'; 
    console.log(webRoot); // '/mysite.com' 
</script> 

webRoot='/mysite.com'と生産webRoot='/'に。 次に、webRoot JavaScript変数を使用して、Webサービスへのアプリケーション相対パスを構築できます。

<script type="text/javascript"> 
    var url = webRoot + '/Web_Services/AjaxWebService.asmx/HelloWorld' 
    console.log(url); // '/mysite.com/Web_Services/AjaxWebService.asmx/HelloWorld' 
</script> 
0

ここGet URL of ASP.Net Page in code-behind

$.ajax({ 
        type: "POST", 
        url: "<%= new Uri(Request.Url,Request.ApplicationPath) %>" + "/Web_Services/AjaxWebService.asmx/HelloWorld", 
        contentType: "application/json; charset=utf-8", 
        dataType: "json", 
        data: "{}", 
        success: AjaxSucceeded 
       }); 


      function AjaxSucceeded(data, status) { 
       // Do whatever... 
      } 
関連する問題