AJAX请求从移动应用程序提供404

时间:2016-07-20 14:07:14

标签: c# asp.net ajax cordova jquery-mobile

我正在使用asp.net,jquery mobile和phonegap编写测试移动应用程序。我有一个简单的表单,您可以在其中输入年份并单击提交。对服务器上的results.aspx.cs页面进行Ajax调用,该页面返回结果并显示在第二页上。

当我在本地服务器上运行移动应用程序时,我收到404错误 - responseText:"无法POST/results.aspx.cs/IsLeapYear↵",状态:404,statusText:& #34;未找到"}。

index.html页面包含以下内容:

<!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>

index.js包含以下内容:

$(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
});
});

results.aspx.cs包含以下内容:

[System.Web.Services.WebMethod]

public static bool IsLeapYear(int year) 
{ 
  return DateTime.IsLeapYear(year); 
}

有人能告诉我我犯了什么错误吗?

0 个答案:

没有答案