问题从sitecore中的WebMethod返回一个字符串

时间:2015-12-21 05:17:12

标签: jquery ajax sitecore webmethod

我的sitecore项目中有以下ajax方法

  $.ajax({
        type: 'POST',
        url: '/layouts/WebMethods.aspx/SaveSubscription',
        data: { hello: 'TestData' },
        cache: false,
        success: function (data) {
            console.log(data);

            $('#newsletter-signup section.form').fadeOut(0);
            $('#newsletter-signup section.thankyou').fadeIn();
        },
        error: function (request, error) {


            alert(error);

            alert("Request: " + JSON.stringify(request));
        }
    });

当按下我的提交按钮时,它会激活上面的内容,然后调用以下webmethod:

    [WebMethod]
    public static string SaveSubscription(string hello)
    {

       return "hello";
    }

然而,当呼叫回来并将数据记录到控制台时,我看到以下内容:

!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head><title>
</title></head>
<body>

<form method="post" action="/layouts/WebMethods.aspx/SaveSubscription" id="form1">

<div class="aspNetHidden">

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjkPFgIeE1ZhbGlkYXRlUmVxdWVzdE1vZGUCAWRkhKZL+kK5CfzPtpuWvMk8nCH9ZzR7QgL8q7X8hM86IDw=" />

</div>
<div>
</div>

<div class="aspNetHidden">

<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="4D781013" />

</div></form>

<!-- Visual Studio Browser Link -->

<script type="application/json" id="__browserLink_initializationData">

{"appName":"Firefox","requestId":"6592f28a894c4e99864e5a04ca41358a"}

</script>

<script type="text/javascript" src="http://localhost:56165/0b7f5b34dc2e4e69a24b14da69a7adb9/browserLink" async="async"></script>

<!-- End Browser Link -->
</body>

</html>
实际上,我希望看到“#34; hello&#34;”这个词,但是我得到了上述内容。

不幸的是我无法调试此调用,因为它是一个sitecore项目,而且我所拥有的PC非常慢/边缘垃圾,并且试图在Visual Studio中附加一个进程大约需要45个项目开始的几分钟,甚至是它的命中和错过。

现在我不确定是否有人可以帮助我,但我想我会试一试。

提前致谢。

更新

我已经将我的ajax方法改为现在看起来像这样:

    $.ajax({
        type: 'POST',
        url: '/layouts/WebMethods.aspx/SaveSubscription',
        data: { hello: 'testData'},
        cache: false,
        dataType: "json", // New
        contentType: "application/json; charset=utf-8", // New
        success: function (data) {
            alert('In Success');

            alert(data);

            $('#newsletter-signup section.form').fadeOut(0);
            $('#newsletter-signup section.thankyou').fadeIn();
        },
        error: function (request, error) {


            alert(error);

            alert("Request: " + JSON.stringify(request));
        }
    });

它现在点击错误方法并发出警告:

Request: {"readyState":4,"responseText":"{\"Message\":\"Invalid JSON primitive: subscribe.\",\"StackTrace\":\"   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\\r\\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\\r\\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\\r\\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\\r\\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\\r\\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)\",\"ExceptionType\":\"System.ArgumentException\"}","responseJSON":{"Message":"Invalid JSON primitive: subscribe.","StackTrace":"   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"},"status":500,"statusText":"Internal Server Error"}

1 个答案:

答案 0 :(得分:0)

$.ajax({
    type: 'POST',
    url: '/layouts/WebMethods.aspx/SaveSubscription',
    data: { hello: 'TestData' },
    cache: false,
    success: function (data) {
        console.log(data.d);

        $('#newsletter-signup section.form').fadeOut(0);
        $('#newsletter-signup section.thankyou').fadeIn();
    },
    error: function (xhr) {
        alert(xhr.responseText);

    }
});