jquery - ASP.NET Web 服务返回 xml 而不是 json

标签 jquery asp.net ajax json web-services

这是我的代码;

$(document).ready(function () {
    $('#btnAddCat').click(function () {
        var cn = $('#txtCategoryName').val();
        alert(cn);
        $.ajax({
            type: 'Post',
            url: "../_ws/news.asmx/InsertCategory",
            data: { catName: + cn  },
            contenttype: 'application/json; charset=utf-8',
            datatype: 'json',
            async: false,
            success: function (msg) {
                alert(msg);
            },
            error: function (msg) {
                alert('failure');
                alert(msg);
            }
        });
    });
});

这是 news.asmx Web 服务中的代码

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Web.Script.Services

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
'<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class News
    Inherits System.Web.Services.WebService


    <WebMethod()> _
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)>
    Public Function InsertCategory(ByVal catName As String) As String
        Dim newid = KTOEOS.NewsCategories.InsertCategory(catName)
        Return "{'c':" & newid & "}".ToString
    End Function

End Class

返回结果为:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">{'c':21}</string>

如果我将 jQuery 部分更改为读取;

...

data: { catName: + cn  },

...

然后我收到一个错误:

System.InvalidOperationException: Missing parameter: catName

我做错了什么?

最佳答案

可能是因为您习惯了 VB 不区分大小写。

尝试这些行(注意国会大厦 T):

 contentType: 'application/json; charset=utf-8',
 dataType: 'json',

关于jquery - ASP.NET Web 服务返回 xml 而不是 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9138072/

相关文章:

asp.net - 无法加载文件或程序集 App_Web_xxxxxxxx 或其依赖项之一

javascript - 使用 AJAX 将 GAE 文件上传到 blobstore

javascript - javascript 中的事件顺序无法正常运行

jquery - 使用 jQuery 调整 HTML 表格单元格宽度

javascript - JQuery 序列化数据输入为空

jquery - 在 JQuery 对话框中执行 AJAX 操作时如何更改光标图标?

jquery - ASP.NET MVC 使用 JQuery 将页面内容加载到 div 中

javascript - jquery ui 中的位置()

c# - 使用 Entity Framework 将具有新角色的用户更新到数据库中

c# - 将 DateTime 插入 sql server 2008 时出现奇怪的错误