我正在尝试使用ajax调用web服务。 Web服务是用vb.net编写的。当我直接使用它的url调用它时它工作正常,但当我使用ajax调用它时它会返回
NetworkError:500内部服务器错误-http://localhost/chart/webService.asmx/HelloWorld。
这是我的网络服务代码:
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class webService
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function HelloWorld() As String
Return "Hello World"
End Function
End Class
这里是javascript代码:
$(document).ready(function() {
var webMethod = 'http://localhost/chart/webService.asmx/HelloWorld';
$.ajax({
type: "POST",
url: webMethod,
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert('all good');
},
error: function(e) {
alert('err');
}
});
});
更新:
我将HelloWorld()更改为以下代码,但仍然收到相同的错误:
<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Function HelloWorld() As String
Dim aa As New System.Web.Script.Serialization.JavaScriptSerializer
Return aa.Serialize("Hello World")
End Function
答案 0 :(得分:0)
使用:
data:{},
而不是:
data:"{}",