gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 01:51:23 26 4
gpt4 key购买 nike

这是我的代码;

$(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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com