C# 来自移动应用程序的AJAX请求提供404
我正在使用asp.net、jquery mobile和phonegap编写一个测试手机应用程序。我有一个简单的表单,您可以在其中输入年份并单击提交。Ajax调用服务器上的results.aspx.cs页面,返回结果并显示在第2页上 当我在本地服务器上运行移动应用程序时,我收到404错误——responseText:“cannotpost/results.aspx.cs/IsLeapYear↵", 状态:404,状态文本:“未找到”} index.html页面包含以下内容:C# 来自移动应用程序的AJAX请求提供404,c#,asp.net,ajax,cordova,jquery-mobile,C#,Asp.net,Ajax,Cordova,Jquery Mobile,我正在使用asp.net、jquery mobile和phonegap编写一个测试手机应用程序。我有一个简单的表单,您可以在其中输入年份并单击提交。Ajax调用服务器上的results.aspx.cs页面,返回结果并显示在第2页上 当我在本地服务器上运行移动应用程序时,我收到404错误——responseText:“cannotpost/results.aspx.cs/IsLeapYear↵", 状态:404,状态文本:“未找到”} index.html页面包含以下内容: <!DOCTYP
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="msapplication-tap-highlight" content="no" />
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width" />
<link rel="stylesheet" href="css/jquery.mobile-1.4.5.min.css">
<link rel="stylesheet" href="css/jqm-icon-pack-fa.css">
<link rel="stylesheet" type="text/css" href="css/index.css" />
<script src="js/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="js/jquery.mobile-1.4.5.min.js" type="text/javascript"></script>
<script>
var SectedCityCode, URL, prov;
$(document).bind('mobileinit', function () {
$.mobile.pushStateEnabled = false;
});
</script>
<title>Hello World</title>
</head>
<body>
<!-- Start of first page -->
<div data-role="page" id="home">
<div data-role="header">
<h1>Test</h1>
</div>
<div data-role="content">
<div data-role="main" class="ui-content">
<div class="ui-body ui-body-a ui-corner-all">
<form id="checkyear" class="ui-body ui-body-a ui-corner-all" data-ajax="false">
<label for="txtYear">Enter the year:</label>
<input type="text" name="txtYear" id="txtYear" value="" />
<input type="button" data-theme="b" name="submit" id="submit" value="Submit">
</form>
</div>
</div>
</div>
<div data-role="footer" data-position="fixed">
<h4>Page footer</h4>
</div>
</div>
<!-- end of first page -->
<!-- start of second page -->
<div data-role="page" id="second">
<div data-theme="a" data-role="header">
<h3>Welcome Page</h3>
</div>
<div data-role="content">
<p id="result"></p>
</div>
<div data-theme="a" data-role="footer" data-position="fixed">
<h3>Page footer</h3>
</div>
</div>
<!-- end of second page -->
<script type="text/javascript" src="js/index.js"></script>
</body>
</html>
$(document).on('pageinit', '#home', function () {
$(document).on('click', '#submit', function () { // catch the form's submit event
if ($('#txtYear').val().length > 0) {
$.ajax({
url: 'http://website.com/results.aspx.cs/IsLeapYear',
data: '{ year: "' + txtYear.value + '"}',
type: 'post',
async: 'true',
dataType: 'json',
success: function (result) {
if (result.status) {
result.val = "Leap Year";
$.mobile.changePage("#second");
} else {
result.val = "Not a Leap Year";
}
},
error: function (request, error) {
// This callback function will trigger on unsuccessful action
alert('Network error has occurred please try again!');
}
});
} else {
alert('Please fill all necessary fields');
}
return false; // cancel original event to prevent form submitting
});
});
[System.Web.Services.WebMethod]
public static bool IsLeapYear(int year)
{
return DateTime.IsLeapYear(year);
}
results.aspx.cs包含以下内容:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="msapplication-tap-highlight" content="no" />
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width" />
<link rel="stylesheet" href="css/jquery.mobile-1.4.5.min.css">
<link rel="stylesheet" href="css/jqm-icon-pack-fa.css">
<link rel="stylesheet" type="text/css" href="css/index.css" />
<script src="js/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="js/jquery.mobile-1.4.5.min.js" type="text/javascript"></script>
<script>
var SectedCityCode, URL, prov;
$(document).bind('mobileinit', function () {
$.mobile.pushStateEnabled = false;
});
</script>
<title>Hello World</title>
</head>
<body>
<!-- Start of first page -->
<div data-role="page" id="home">
<div data-role="header">
<h1>Test</h1>
</div>
<div data-role="content">
<div data-role="main" class="ui-content">
<div class="ui-body ui-body-a ui-corner-all">
<form id="checkyear" class="ui-body ui-body-a ui-corner-all" data-ajax="false">
<label for="txtYear">Enter the year:</label>
<input type="text" name="txtYear" id="txtYear" value="" />
<input type="button" data-theme="b" name="submit" id="submit" value="Submit">
</form>
</div>
</div>
</div>
<div data-role="footer" data-position="fixed">
<h4>Page footer</h4>
</div>
</div>
<!-- end of first page -->
<!-- start of second page -->
<div data-role="page" id="second">
<div data-theme="a" data-role="header">
<h3>Welcome Page</h3>
</div>
<div data-role="content">
<p id="result"></p>
</div>
<div data-theme="a" data-role="footer" data-position="fixed">
<h3>Page footer</h3>
</div>
</div>
<!-- end of second page -->
<script type="text/javascript" src="js/index.js"></script>
</body>
</html>
$(document).on('pageinit', '#home', function () {
$(document).on('click', '#submit', function () { // catch the form's submit event
if ($('#txtYear').val().length > 0) {
$.ajax({
url: 'http://website.com/results.aspx.cs/IsLeapYear',
data: '{ year: "' + txtYear.value + '"}',
type: 'post',
async: 'true',
dataType: 'json',
success: function (result) {
if (result.status) {
result.val = "Leap Year";
$.mobile.changePage("#second");
} else {
result.val = "Not a Leap Year";
}
},
error: function (request, error) {
// This callback function will trigger on unsuccessful action
alert('Network error has occurred please try again!');
}
});
} else {
alert('Please fill all necessary fields');
}
return false; // cancel original event to prevent form submitting
});
});
[System.Web.Services.WebMethod]
public static bool IsLeapYear(int year)
{
return DateTime.IsLeapYear(year);
}
有人能告诉我我犯了什么错误吗
Joe你的网站可以通过其他方式访问吗?尝试使用HTTP客户端在你的URL上测试你的请求头和请求体,这在这些情况下是一个非常有用的工具。很好的工具。当我使用GET时,我获得状态200,但使用POST时,我找不到404。你知道为什么吗?如果你无法
发布,它在浏览器/@citm09中工作吗
通常我会检查您的本地环境设置,可能您的web.config不允许按照KB设置POST
标题,我为POST和GET in web.config添加了行,但仍然显示相同的错误消息。是否可以通过其他方式访问您的网站?尝试使用HTTP客户端在您的URL测试您的请求标题和正文,这是一个v在这些情况下非常有用的工具很好的工具。当我使用GET我得到状态200 OK但使用POST我得到404找不到。你知道为什么吗?如果你不能POST
正常情况下我会检查你的本地环境设置,也许你的web.config不允许POST
标题,根据知识库,我添加了f行或在web.config中发布和获取,但仍然显示相同的错误消息。