后端为C# webservice 在web.config文件中添加:

  1. <configuration>节点下添加
    <system.webServer> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Methods" value="OPTIONS,POST,GET"/> <add name="Access-Control-Allow-Headers" value="x-requested-with,content-type"/> <add name="Access-Control-Allow-Origin" value="*" /> </customHeaders> </httpProtocol> </system.webServer>
  2. <system.web>节点下添加
    <webServices> <protocols> <add name="HttpSoap"/> <add name="HttpPost"/> <add name="HttpGet"/> <add name="Documentation"/> </protocols> </webServices>

前端JS发起ajax请求时,async: false,

				var params = {};
                params.username = data.field.userName;
                params.password = data.field.password;

                $.ajax({
                    async: false,
                    type: "post",
                    contentType: "application/json;charset=UTF-8", //类型必填
                    url: "https://www.gemai/WeChatData.asmx/login",
                    data: JSON.stringify(params),
                    dataType: "json",
                    success: function (data) {
                        console.log(data);
                        var json_result = JSON.parse(data.d);
                        if (json_result.code == 200) {
                            alert('登录成功');
                            location.href = 'index.html';
                            //alert(json_result.result[0].account_id);
                        } else {
                            alert('登录失败');
                        }
                    },
                    error: function (data) {
                        alert('登录失败');
                    }
                })
                return false;
            });

更多推荐

AJAX跨域发起https请求配置方案