顯示具有 javascript 標籤的文章。 顯示所有文章
顯示具有 javascript 標籤的文章。 顯示所有文章

2012年9月27日 星期四

jQuery ajax post 在IIS中無法存取 WebService (*.asmx)



有某段程式碼如下,在本機測試時沒問題,一放到IIS上就失敗

$.ajax({
type: "POST",
url: "/Services/CommonService.asmx/QueryNation",
data: '',
async: false,
success: function (result) {
var msg = $(result.lastChild).text();
alert(msg);
}
});


有兩種方式解決:

1. 在IIS上設定虛擬目錄,指向存放webservice的資料夾

2. 在web.config上設定webService區段,如下:
<!-- 設定允許Service存取 -->
<configuration>
 <system.web>
    <webServices>
      <protocols>
        <add name="HttpPost" />
      </protocols>
    </webServices>
 </system.web>
</configuration>

2011年11月24日 星期四

submit特定元素的資料

有時候因為某些不可抗拒的因素,而只能將某些html元素寫在某個form當中

例子如下:

<form id="form1" method="post" action="post_target.html">
<input id="txtName" type="text" name="Name">
<input id="txtTel" type="text" name="Tel">
<input id="txtAddr" type="text" name="Address">
<input id="btnSend" type="submit" value="送出">
</form>
此時一按下送出,就會將整個form1的資料送出去

如果希望只送出Name欄位的資料出去該怎麼做呢?
以下為了程式碼的精簡,使用了jQuery來處理dom物件,改寫後的code如下
<form id="form1"> // 將 method="post" action="post_target.html"拿掉
<input id="txtName" type="text" name="Name">
<input id="txtTel" type="text" name="Tel">
<input id="txtAddr" type="text" name="Address">
<input id="btnSend" type="button" value="送出" onclick="doPost();" />
</form>

<script type="text/javascript">
function pay() {
var name= $('#txtName').val();
var form = $('<form method="post" action="post_target.html"></form>');
form.append('<input type="hidden" name="Name" value="' + name + '" />"');
$('body').append(form);
form.submit();
}
</script>

2011年9月28日 星期三

WebSite 與 WebApplication 偵錯時的路徑問題

當使用WebSite專案時,按下偵錯(F5)後,產生的網址會像是這樣
http://localhost:7788/WebSite1/index.aspx

當使用WebApplication專案時,按下偵錯(F5)後,產生的網址會像是這樣
http://localhost:7788/index.aspx


假設在根目錄下有一的名為MyJS.js的javascript文件需要引用
我們在index.aspx裡面下入以下程式碼
<script type="text/javascript" src="/MyJS.js"></script>

當使用WebApplication專案時,一切都沒有問題
若使用WebSite專案時,就會發生錯誤

原因應該很明顯了 ' / ' 代表網站的根目錄
因為WebSite專案自動幫我們加了一個WebSite1的虛擬路徑
而導致錯誤的發生



解決方法:
1. 打開專案的屬性視窗
2. 將'虛擬路徑'屬性 由原先的 ' /WebSite1 '改成' / '
3. 完成