2017-03-04 68 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

相关问题