[英]jquery ajax call to JsonResult controller method results in 404 on IIS6
I've been pulling my hair out this morning trying to figure this out. 今天早上我一直在拔头发试图解决这个问题。
I have a simple jquery json request to a jsonresult action on my controller. 我对我的控制器上的jsonresult操作有一个简单的jquery json请求。 When I run this on my local machine (IIS7), it works fine.
当我在我的本地计算机(IIS7)上运行它时,它工作正常。 When I deploy to a dev machine running IIS6, I get a 404 error.
当我部署到运行IIS6的开发机器时,我收到404错误。
script: 脚本:
$(function() {
$('#search').click(function() {
var zip = $('#zip').val();
$.ajax({
type: "GET",
url: "/Customer/GetCityStateFromZip",
data: { zipcode: zip },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$("#stateList").setCityState(msg);
}
});
});
});
Controller: 控制器:
public JsonResult GetCityStateFromZip(String zipcode)
{
List<CityState> list = new List<CityState>();
foreach (var item in dt)
{
list.Add(new CityState(){City = item.City, StateCode = item.StateCode, StateName = item.StateName});
}
return this.Json(list);
}
Request Data: 请求数据:
GET /Customer/GetCityStateFromZip?zipcode=85215 HTTP/1.1
Host: mydevserver
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6
Accept: application/json, text/javascript, */*
Accept-Language: en-us,es-mx;q=0.7,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
X-Requested-With: XMLHttpRequest
Referer: http://mydevserver/Customer/Entry
Response Data: 响应数据:
HTTP/1.1 404 Not Found
Date: Wed, 30 Jun 2010 18:01:06 GMT
Content-Length: 1635
Content-Type: text/html
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
On my dev server running IIS6, I have set a wildcard mapping (C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\aspnet_isapi.dll) and have the "Verify that file exists" unchecked. 在我运行IIS6的开发服务器上,我设置了一个通配符映射(C:\\ WINDOWS \\ Microsoft.NET \\ Framework \\ v2.0.50727 \\ aspnet_isapi.dll)并取消选中“验证该文件是否存在”。
All other aspects of my MVC site work just fine. 我的MVC网站的所有其他方面都可以正常工作。 I'm using jquery all over the place (validation, animation, etc) and that's working fine.
我在整个地方使用jquery(验证,动画等)并且工作正常。 I just can't seem to get beyond this ajax issue.
我似乎无法超越这个ajax问题。
Is there some other setting or mapping issue that I need to address on the IIS6 machine? 我需要在IIS6机器上解决一些其他设置或映射问题吗? Perhaps IIS6 doesn't know how to route this request?
也许IIS6不知道如何路由这个请求?
Alright, I figured it out with the help from another SO post . 好吧,我在另一篇SO帖子的帮助下想出来了。
The problem was with the URL being passed. 问题在于传递了URL。 It's obviously different from my machine, and the server I deployed to.
它明显不同于我的机器,以及我部署的服务器。 I'm embarrassed that I didn't think about this.
我很尴尬,我没有想到这一点。
I changed the $.ajax call from this: 我改变了$ .ajax调用:
url: "/Customer/GetCityStateFromZip"
To this, which is using Url.Action to the correct full path: 对此,使用Url.Action到正确的完整路径:
var url = '<%= Url.Action("GetCityStateFromZip","Customer") %>';
url: url
And all is well. 一切都很好。
For extensionless urls in IIS 6 you need a wildcard mapping. 对于IIS 6中的无扩展URL,您需要通配符映射。 Read this blog post for more details on setting this up.
阅读此博客文章 ,了解有关设置的更多详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.