Ajax函数返回“请求在此上下文中不可用”

时间:2016-06-20 13:18:59

标签: jquery asp.net ajax vb.net webforms

我正在开发一个VB.NET webform项目,我正在尝试使用ajax调用一个函数,然后返回一个以html显示的对象。

这是我的ajax电话:

  $(document).ready(function () {
    msgClickedClass.click(function (event) {
        msgID = event.target.id;
        //Get message body
        $.ajax({
            type: "POST",
            url: "/secure/PortalMessages.aspx/PrintMessageContent",
            data: JSON.stringify({ "msgID": msgID }),
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (data) {
                // $("#dialogText").html(data.d.message);

                alert("success");
            },
            error: function (xhr, status, error) {
                alert('Error: ' + status + '\nError Text: ' + error);
            }
        }) //end ajax


    }); //end click function
  });

这是我的vb函数由ajax调用:

<WebMethod()> _
Public Shared Function PrintMessageContent(msgID As Integer) As Messages
    Dim msg As New Messages

    Dim db As New DB_SQL()

    msg = db.GetSpecificMessageSent(msgID)
    Return msg
End Function

当我调试代码时,我确实有一个应该返回的Messages对象,但是ajax函数失败并返回500错误,而不是“请求在此上下文中不可用”。

当我更改PrintMessageContent以返回字符串而不是对象时,ajax函数成功。

任何建议都将不胜感激。

更新:以下是我的Messages类的内容

    Public Class Messages
Private _TimeStamp As DateTime
Private _Subject As String
Private _Message As String
Private _Agent As String
Private _Id As Integer
Private _MessageViewHistory As List(Of MessagesViewed)

Public Property TimeStamp() As DateTime
    Get
        Return _TimeStamp
    End Get
    Set(value As DateTime)
        _TimeStamp = value
    End Set
End Property

Public Property Subject() As String
    Get
        Return _Subject
    End Get
    Set(value As String)
        _Subject = value
    End Set
End Property

Public Property Message() As String
    Get
        Return _Message
    End Get
    Set(value As String)
        _Message = value
    End Set
End Property

Public Property Agent() As String
    Get
        Return _Agent
    End Get
    Set(value As String)
        _Agent = value
    End Set
End Property

Public Property Id() As Integer
    Get
        Return _Id
    End Get
    Set(value As Integer)
        _Id = value
    End Set
End Property



Public Property MessageViews() As List(Of MessagesViewed)
    Get
        Return _MessageViewHistory
    End Get
    Set(value As List(Of MessagesViewed))
        _MessageViewHistory = value

    End Set
End Property

Public Property MessageHTMLFormat() As String
    Get
        Dim retn As String
        retn = Me.Message.Replace(vbCrLf, "<br />")
        Return retn

    End Get
    Set(value As String)

    End Set
End Property



End Class

更新#2:新功能:

 <WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Shared Function PrintMessageContent(msgID As Integer) As String
    Dim msg As New Messages

    Dim db As New DB_SQL()

    msg = db.GetSpecificMessageSent(msgID)

    Dim serializer As New JavaScriptSerializer()
    Dim serializedResult = serializer.Serialize(msg)
    Return serializedResult
End Function

2 个答案:

答案 0 :(得分:0)

所有你定义的返回数据类型为JSON,因此你需要指定返回类型:

<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _

在文件的顶部,您需要指定脚本服务

Inherits System.Web.Services.WebService

最后你必须将对象转换为JSON

 Dim serializer As New JavaScriptSerializer()
 Dim serializedResult = serializer.Serialize(db.GetSpecificMessageSent(msgID))
 Return serializedResult

<强>更新 我注意到你从ajax JSON.stringify({ "msgID": msgID })发送对象但从不使用反序列化。这样做的目的是什么?无论如何你只需发送ID就可以改变你的代码:

url: "/secure/PortalMessages.aspx/PrintMessageContent",
data: { "msgID": msgID },
dataType: 'json',

这也应该有所帮助。

实际上如果你还是喜欢使用stringify格式中的一个小错误。我在webservice中遇到了同样的错误。试试这个:

data: "{'msgID':'" + JSON.stringify(msgID) + "'}",

此格式可帮助您避免类似以下错误:无法解析JSON原语。

答案 1 :(得分:0)

我发现问题出在我的Messages类中。出于某种原因,当我查看Messages对象时,它返回的不仅仅是指定的属性。它提供了诸如Buffer, Content-Type, etc之类的东西。

我将Messages类更改为this,当我将鼠标悬停在Messages对象上时,它只会提供指定的属性。

希望这可以节省一些时间和挣扎。

Public Class Messages

Public TimeStamp As DateTime
Public Subject As String
Public Message As String
Public Agent As String
Public Id As Integer
Public MessageViews As List(Of MessagesViewed)

End Class