2017-03-04 74 views
0

Ajax調用本地主機上工作,但不會對azure-webites.net

<script type="text/javascript"> 
 
    
 
    $(document).ready(function() { 
 

 
     function validemail(isemail) { 
 
      var emailReg = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/; 
 
      return emailReg.test(isemail); 
 
     } 
 

 
     $("#<%=txtEmail.ClientID %>").blur(function() {    
 
      if ($("#<%=txtEmail.ClientID %>").siblings().size() > 0) { 
 
       $("div").remove(".tooltips"); 
 
      } 
 
     }); 
 

 

 
     $("#btnSubmit").click(function() { 
 
      var name = $("#<%=txtName.ClientID %>").val(); 
 
      var email = $("#<%=txtEmail.ClientID %>").val(); 
 
      var message = $("#<%=txtMessage.ClientID %>").val(); 
 
      if (name != '' && email != '' && message != '') { 
 
       if (validemail(email)) { 
 
        $.ajax({ 
 
         type: "POST", 
 
         contentType: "application/json; charset=utf-8", 
 
         url: "http://abcname.azurewebsites.net/Contact.aspx/InsertData", 
 
         data: "{'customername':'" + name + "','customeremail':'" + email + "','customermessage':'" + message + "'}", 
 
         dataType: "json", 
 
         success: function (data) { 
 
          var obj = data.d; 
 
          if (obj == 'true') { 
 
           $("#<%=txtName.ClientID %>").val(''); 
 
           $("#<%=txtEmail.ClientID %>").val(''); 
 
           $("#<%=txtMessage.ClientID %>").val(''); 
 
           alert('Details submitted successfully'); 
 
          } 
 
         }, 
 
         error: function (result) { 
 
          alert("An error occur while submitting details."); 
 
         } 
 
        }); 
 
       } 
 

 
       else { 
 
        $("#<%=txtEmail.ClientID %>").focus(); 
 
        $("<div class='tooltips'><span>Invalid Email Address</span></div>").insertAfter("#<%=txtEmail.ClientID %>"); 
 
       } 
 
      } 
 
      else { 
 
       alert('Please fill all the fields');    
 
      } 
 
     }); 
 
    }); 
 

 
</script>

上面代碼中的本地主機上正常使用,但它並沒有在服務器端。如果將有上的.cs任何錯誤文件,那麼它會顯示警告框,但它甚至不顯示警告框,

+0

也許[CORS](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing)是罪魁禍首? – 4c74356b41

+0

可否請您詳細說明。我該如何解決這個問題 – user3581927

回答

0

網址「同時提交詳細信息發生錯誤」:「http://abcname.azurewebsites.net/Contact.aspx/InsertData」,

根據URL,我懷疑您是使用WebMethod來處理WebForms應用程序中的AJAX請求。由於URL中有.aspx後綴,因此請確保已註釋掉以下代碼,該代碼默認存在於RouteConfig.cs中。

//settings.AutoRedirectMode = RedirectMode.Permanent; 

,但它甚至不顯示警告框,

VAR OBJ = data.d 「同時提交詳細信息出現錯誤」;

原因可能是響應回來了,但它不包含名爲d的屬性或其值不等於'true'。我建議使用一個工具來查看詳細的響應消息。 Fiddler是可以幫助你做到的常用工具。

此外,您是否發送來自不同域名的AJAX請求爲'http:// abcname .azurewebsites.net'?例如,您發佈的代碼位於名爲「http:// defname .azurewebsites.net」的網站中。如果確實如此,則需要在abcname web應用程序中配置CORS。以下步驟供您參考。

  1. 在Azure門戶中,打開Web應用程序abcname。
  2. 在菜單欄上,單擊CORS。
  3. 在'Allowed Regions'文本框中輸入域名。
  4. 點擊[保存]按鈕保存您的所有操作。

enter image description here

相關問題