首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >从asp.net项目中的html页面调用ajax请求

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

Stack Overflow用户
提问于 2015-07-17 14:38:31
回答 1查看 1.1K关注 0票数 1

我无法从我的visual studio项目中的html页面获得一个简单的ajax示例。它在webform (Aspx)上工作得很好:

..。webform1.aspx和form1.html。

代码语言:javascript
代码运行次数:0
运行
复制
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);
        }

..。webform1.aspx和form1.html。

代码语言:javascript
代码运行次数:0
运行
复制
<input id="btnGetTime" type="button" value="Show Current Time"
                   onclick="ShowCurrentTime()" />

..。json.aspx ...

代码语言:javascript
代码运行次数:0
运行
复制
[WebMethod]
        public static string GetCurrentTime(string name)
        {
            return "Hello " + name + Environment.NewLine + "The Current Time is: "
                + DateTime.Now.ToString();
        }

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

更新#1

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

{“消息”:“无效JSON原语: bob.","StackTrace":”at JSON at .

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-17 16:04:37

你的儿子畸形了。字符串需要在引号中。把bob放在引号里,你应该是好的。不过,我不知道它为什么要在ASP.NET页面上工作,除非它有引号。

代码语言:javascript
代码运行次数:0
运行
复制
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);
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31478396

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档