从asp.net项目内的html页面调用ajax请求

从asp.net项目内的html页面调用ajax请求,asp.net,ajax,json,Asp.net,Ajax,Json,我无法从VisualStudio项目中的html页面获得一个简单的ajax示例。它可以在网络表单(aspx)中正常工作: 。。。 webform1.aspx和form1.html 。。。 webform1.aspx和form1.html 如果我把代码放在webform1.aspx中,它可以正常工作。如果我把它放在form1.html上,json.aspx不会返回任何内容。我正在使用vs2013从我的机器上以调试模式运行项目。任何帮助都将不胜感激 更新#1 我检查了fiddler,服务器返回以

我无法从VisualStudio项目中的html页面获得一个简单的ajax示例。它可以在网络表单(aspx)中正常工作:

。。。 webform1.aspx和form1.html

。。。 webform1.aspx和form1.html

如果我把代码放在webform1.aspx中,它可以正常工作。如果我把它放在form1.html上,json.aspx不会返回任何内容。我正在使用vs2013从我的机器上以调试模式运行项目。任何帮助都将不胜感激

更新#1

我检查了fiddler,服务器返回以下结果(500):


{“Message”:“无效的JSON原语:bob.”,“StackTrace”:“at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n在…

您的json格式不正确。字符串需要加引号。将
bob
加引号,您应该会很好。我不确定它为什么会在ASP.NET页面上工作,除非它有引号

function ShowCurrentTime() {
        alert("before json");
        $.ajax({
            type: "POST",
            url: "json.aspx/GetCurrentTime",
            data: "{name: \"bob\" }",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (response) {
                alert(response.d);
            }

        });
        alert("after json");
    }

    function OnSuccess(response) {
        alert(response.d);
    }

form1.html与webform1.aspx在同一目录下吗?在浏览器中打开开发人员工具,在Javascript控制台中查找任何错误。form1.html加载jquery.js?是的,form1.html与webform1.aspx在同一目录下。是的,jquery已加载,通过调用if(window.jquery)确认{//jQuery已加载警报('jQuery已加载');}检查firefox浏览器控制台,按下按钮时控制台中没有任何错误。我只是得到了前/后警报。在IE中也尝试过。老兄,我觉得自己很笨。谢谢老兄。如果这是一个愚蠢的错误,而不是需要几周才能重新编码,那就更好了:)
<input id="btnGetTime" type="button" value="Show Current Time"
                   onclick="ShowCurrentTime()" />
[WebMethod]
        public static string GetCurrentTime(string name)
        {
            return "Hello " + name + Environment.NewLine + "The Current Time is: "
                + DateTime.Now.ToString();
        }
function ShowCurrentTime() {
        alert("before json");
        $.ajax({
            type: "POST",
            url: "json.aspx/GetCurrentTime",
            data: "{name: \"bob\" }",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (response) {
                alert(response.d);
            }

        });
        alert("after json");
    }

    function OnSuccess(response) {
        alert(response.d);
    }