繁体   English   中英

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

[英]ASP.NET web service returning xml instead of json

这是我的代码;

$(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',

从您的Web服务代码中取消注释以下行。

<System.Web.Script.Services.ScriptService()>

为什么要将async设置为false 这将完全停止浏览器,直到服务器响应。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM