ajax 调用web service 报错

JERRY_LIU 2016-01-20 04:36:09
写了一个简单的web service, hello ,然后用ajax来调用,执行到 success: function (result) 时就报错internal server error.

请帮忙看看是不是哪里写错了。

web service code:

<WebMethod(Description:="test with hello", enablesession:=False, messagename:="hello")>
Public Function hello() As String
Return "hello"

End Function


ajax code:

<script type="text/javascript">
function hellotest() {
$.ajax({
type: 'POST',
contentType: 'application/json',
url: 'http://localhost:42459/Service1.asmx/hello',
data: '{}',
dataType: 'json',
success: function (result) {
alert(result.d);
},
error: function (xhr, status, errMsg) {
alert(errMsg);
}
});

}
</script>
...全文
144 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
JERRY_LIU 2016-01-25
  • 打赏
  • 举报
回复
原因已经找到了,是因为在web service 中有一段代码未启用,所以无法被ajax 调拔。 就是这一句: <System.Web.Script.Services.ScriptService()> _
hookee 2016-01-21
  • 打赏
  • 举报
回复
1 确保ajax请求同源,页面也在 http://localhost:42459/ 上 2 从直接调用的结果看返回的是 xml,不是json 3 用的是 POST方法,确保.../hello 是允许POST动作的,直接访问是GET
JERRY_LIU 2016-01-21
  • 打赏
  • 举报
回复
楼上朋友,我改了一下Web service,将返回的字符串格式改了一下,代码如下: 再次调用后,返回的值是 <string>{'Hello'}</string> 这个Web service 和web form 是在同一目录下的,是否算同源?第三点看不懂,怎样允许web service POST?

    <WebMethod(Description:="test with hello", enablesession:=False, messagename:="hello")>
    Public Function hello() As String
        Dim str As String
        str = "{'Hello'}"
        Return str

    End Function
JERRY_LIU 2016-01-20
  • 打赏
  • 举报
回复
直接调用可以的,在IE上调用后返回内容如下: <string>hello</string>
hch126163 2016-01-20
  • 打赏
  • 举报
回复
http://localhost:42459/Service1.asmx/hello 1、直接请求,看看是否能正确执行 2、 响应到浏览器的是一个json 字符串??

87,992

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧