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

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月22日 星期四

當網站部屬到IIS後 使用jQuery ajax呼叫webService(.asmx)失敗

在本機開發的時候可以正常呼叫,一但部署到IIS上面就發成錯誤
檢查error status為404錯誤

解決方式:
在web.config加入


<configuration>
  <system.web>
    <webServices>
       <protocols>
          <add name="HttpPost" />
       </protocols>
    </webServices>
  </system.web>
</configuration>