jQuery ajax返回整个页面内容而不是结果

时间:2013-03-28 19:10:08

标签: jquery asp.net ajax

我必须为使用Synapse内容管理系统的VS 2008(.Net 3.5)网站添加功能。经过几个小时的挣扎,我从零开始,以下工作在一个全新的网络项目中。

这是客户端脚本和html:

<script type="text/javascript">
function ClickedIt() {
    $.ajax({
        type: "POST",
        url: "Default.aspx/FromClient",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            alert(msg.d);
        }
    });
} // ClickedIt
function ClickedItDeeper() {
    $.ajax({
        type: "POST",
        url: "/Deeper/ActionsController.aspx/FromClient",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            alert(msg.d);
        }
    });
} // ClickedItDeeper
</script>

<asp:Button ID="btnClickMe" runat="server" Text="Click Me - Server" />&nbsp;&nbsp;
<asp:Button ID="btnViaClient" runat="server" Text="Click Me - Client" OnClientClick="ClickedIt(); return false;" />&nbsp;&nbsp;
<asp:Button ID="btnDeeper" runat="server" Text="Click Me - Deeper" OnClientClick="ClickedItDeeper(); return false;" />

更好的是,我必须用VB编写它,所以这里是我正在调用的两个不同的测试。一个是我正在运行的实际页面(Default.aspx),另一个是我喜欢这样做以保持方法在一起的方式(ActionsController.aspx):

Imports System.Web.Services

  Partial Public Class _Default
  Inherits System.Web.UI.Page

Protected Sub btnClickMe_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnClickMe.Click
    Me.lblResult.Text = "From the Server Side: " + DateTime.Now.ToString()
End Sub

<WebMethod()> _
Public Shared Function FromClient() As String
    Return "Via the Client: " + DateTime.Now.ToString()
End Function

End Class

ActionsController是:

Imports System.Web.Services

Partial Public Class ActionsController
Inherits System.Web.UI.Page

<WebMethod()> _
Public Shared Function FromClient() As String
    Return "Via the Deeper Client: " + DateTime.Now.ToString()
End Function

End Class

以上所有工作都像是孤立网站中的冠军。但是,当我尝试在此CMS站点中执行相同操作时,我得到:parseerror:无效字符。在看了很多很多文章之后(没有修好),有人提到过,也许telerik甚至CMS软件都覆盖了JSON.parse,看起来好像是。我强迫它不使用另一个,现在它正在使用json2.js。当我提出:

alert(text);

就在json2.js文件中:

JSON.parse = function(text, reviver) {

它向我显示了整个html页面内容,这是我用它调用它时找不到的页面:

$.ajax({
type: "POST",
url: "ActionsController.aspx/FromClient",
data: "{}",
contentType: "application/json",
dataType: "json",
success: function(msg) {
    $("#lblResult").html(msg);
    alert("success");
},
error: function(xhr, textStatus, errorThrown) {
    alert("Ajax error on " + this.url +
        "\nStatus: " + textStatus +
        "\nError: " + errorThrown +
        "\nText: " + xhr.statusText +
        "\nxhrStatus: " + xhr.status
    );
}
});

给我一​​个SyntaxError错误:JSON.parse,但文本值为OK,状态为200。

我已经将URL更改为没有前导/并且添加了en-US,并且没有前导斜杠都无效。我不确定找不到的页面是否合法。最初,ActionsController页面位于文件夹中。我已将其移至网站的根目录。

这是我在VS2010上做过很多次的事情。它可以在VS2008中运行,但我认为有些东西正在阻碍并且通过这个CMS将某些东西附加到URL或其他东西。当它出错时,我确实让它传递了this.url,它是:/ActionsController.aspx/FromClient,这对我有用。

以下是损坏网站的实际ActionsController代码:

Imports System.Web.Services

Partial Public Class ActionsController
Inherits System.Web.UI.Page

<WebMethod()> _
 Public Shared Function FromClient() As String
    Return "Via the Deeper Client: " + DateTime.Now.ToString()
End Function

<WebMethod()> _
Public Shared Function Multiply(ByVal Input As Integer) As String
    Return "Via the Deeper Client: " + DateTime.Now.ToString()
End Function

End Class

再过一个小时,这将是一整天的搜索,调整和测试可能非常简单但隐藏在telerik和synapse的“功能”层和层之下的东西。

请完成我的一天!

1 个答案:

答案 0 :(得分:0)

好的,在与开发人员进行了一些交谈之后,我获得了足够的信息来解决这个问题。基本上我必须做两件事才能让它发挥作用。一,我必须覆盖Global.asax文件中的BeginRequest,然后在web.config中添加一些值。

如果有其他人遇到这样的问题,这是完整的结果。

在.aspx页面上,这是jQuery ajax调用:

function LoadAllScannedDocumentCounts() {
    var labels = $("[data-DocumentFolder]");

    $.each(labels, function(index, label) {
        var ComplaintNumber = $(this).attr("data-DocumentFolder");
        $(this).prepend("<img id='spinImage" + ComplaintNumber + "' src='/ComplaintsReports/Images/spinner.gif' />");

        $.ajax({
            url: "/Exclusions/ActionsController.aspx/GetScannedDocumentCount",
            type: "POST",
            data: "{ " +
                    "       'ComplaintNumber': '" + ComplaintNumber + "'" +
                " }",
            contentType: "application/json",
            dataType: "json",
            success: function(data) {
                $(label).text(data.d);

                if (data.d > 0)
                    $(label).addClass("badge badge-info");
                else
                    $(label).addClass("badge");
            }, // Success
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert("Error: " + errorThrown + "\nURL: " + this.url);
            } // Error
        }); // Ajax
    }); // foreach of the checked check boxes
} // LoadAllScannedDocumentCounts - Method

web.config补充:

<system.web.extensions>
    <scripting>
        <webServices>
            <authenticationService enabled="true" />
        </webServices>
    </scripting>
</system.web.extensions>

Global.asax文件:

导入System.Web.SessionState

Public Class Global_asax
Inherits SynapseBaseSite46.Global

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.BeginRequest
    Dim app As HttpApplication = DirectCast(sender, HttpApplication)
    Dim con As HttpContext = app.Context

    ' Skip any calls to the ActionsController
    If (Not con.Request.FilePath.ToLower().Contains("exclusions/actionscontroller.aspx")) Then
        MyBase.Application_BeginRequest(sender, e)
    End If
End Sub

End Class

最后,“ActionsController”页面上有WebMethods:

Imports System.Web.Services
Imports System.IO

Partial Public Class ActionsController
Inherits System.Web.UI.Page

<WebMethod()> _
 Public Shared Function GetScannedDocumentCount(ByVal ComplaintNumber As String) As Integer
    Dim Result As Integer = 0
    Dim SearchPath As String = ConfigurationManager.AppSettings("ScannedImagesShare") & "\"

    Try
        For Each dir As DirectoryInfo In New DirectoryInfo(SearchPath & ComplaintNumber.Replace("-", "")).GetDirectories()
            Dim files As FileInfo() = dir.GetFiles("*.pdf")
            Result += files.Count()
        Next
    Catch dex As DirectoryNotFoundException
        ' do nothing
    Catch ex As Exception

    End Try

    Return Result
 End Function

End Class