我一直在追查一个问题,并决定对这个问题做出最基本的再现,希望简化的问题能帮助我解决这个问题。
在通过JavaScript中的AJAX发布到Web服务时,我收到了401错误。 我将包括尽可能多的信息以及我尝试过的所有故障排除步骤。
我在我的网站的根目录上创build了一个testing页面,并尝试使用纯JS和jQuery来进行AJAX调用,结果相同。 我已经包括jquery注释掉了:
页:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//" "http://www.w3.org/TR/html4/loose.dtd"> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script> <script type="text/javascript"> /*$(document).ready(function() { $('#button').click(function() { var ID = 2; var firstname = $('#First_Name').val(); var lastname = $('#Last_Name').val(); $.ajax({ type: 'POST', url: '/service/name/service.aspx/ChangeName', contentType: 'application/json; charset=utf-8', dataType: 'json', data: '{ "ID": "' + ID + '", "firstName": "' + firstname + '", "lastName": "' + lastname + '" }', success: function() { console.log('success'); }, error: function(d) { console.log(d.responseText); } }); }); });*/ function runAjax() { //Create Http request depending on browser var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest();} else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");} xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState==4 && xmlhttp.status==200){ document.getElementById("myDiv").innerHTML=xmlhttp.responseText;} } // Function to create var url = "/service/name/service.aspx/ChangeName"; xmlhttp.open("POST",url,true); xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8"); var data=JSON.stringify({ "ID": "2", "firstName": "John", "lastName": "Doe" }); xmlhttp.send(data); } </script> </head> <body> <div id="myDiv"> <table width="100%" cellpadding="3" cellspacing="0"> <tbody> <tr> <td width="34%"> <div align="right"> <strong>First Name: </strong> </div> </td> <td> <div> <input name="First_Name" type="text"id="First_Name" size="20"> </div> </td> </tr> <tr> <td> <div align="right"> <strong>Last Name: </strong> </div> </td> <td> <input name="Last_Name" type="text" id="Last_Name" size="20"> </td> </tr> </tbody> </table> <div align="center"> <input id="button" type="button" value="Update Student Details" onclick="runAjax();" style="margin:5px;"> </div> </div> </body> </html>
除IE10以外的每个浏览器,这工作正常。 提琴手显示,IE获得401响应。
请求标题 提琴手
POST http://dev.mysite/service/name/service.aspx/ChangeName HTTP/1.1 Accept: */* Content-Type: application/json; charset=utf-8 Referer: http://dev.mysite/test_page.html Accept-Language: en-US Accept-Encoding: gzip, deflate User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0) Connection: Keep-Alive Content-Length: 46 DNT: 1 Host: dev.mysite Pragma: no-cache {"ID":"2","firstName":"John","lastName":"Doe"}
响应头文件 提琴手
HTTP/1.1 401 Unauthorized Content-Type: application/json; charset=utf-8 Server: Microsoft-IIS/7.0 jsonerror: true WWW-Authenticate: Negotiate WWW-Authenticate: NTLM X-Powered-By: ASP.NET Date: Thu, 07 Mar 2013 01:12:05 GMT Content-Length: 105 Proxy-Support: Session-Based-Authentication
我在服务器上启用了“失败的请求跟踪”,这是它为我提供的信息:
失败的请求日志 iis
1. -GENERAL_REQUEST_START
2. -FILTER_START
3. -FILTER_END
4. -AspNetStart
5. -GENERAL_REQUEST_END
我通过简化ajax请求发现的一件事是,它不仅是一个Windows 8的问题,兼容模式也不起作用。
我已经尝试禁用IIS中的内核模式身份validation,我已经检查了Web服务文件夹的权限, 遵循这些build议 ,甚至尝试将Web服务放在与页面相同的目录中。 所有产生相同的结果。 任何方向或援助将不胜感激。