0

我正在处理一个 SharePoint 项目。我想使用 JQuery 从主页进行 ajax 调用。但是,当我拨打电话时,我收到“Array.prototype.slice: 'this' is not a JavaScript object”错误。

这是我正在使用的文件广告代码:homepage.js/

$(document).ready(function () {

   Ajax.GetWeather();
});

Ajax.js/

    ns.GetWeather = function () {
   alert("yep");
    $.ajax({
        data: $.extend({
            Function: "GetWeather"
        }),
        type: 'POST',
        success: function (json) {

            //$('#ContentMain').append(json.LoginMainHtml);
            //$('#ContentSide').append(json.LoginSideHtml);
            alert("hmmmm");
            ns.HideUpdateStatus();
        }
    });
};

14/模板/布局/GWR/Ajax.ashx/

    <%@ WebHandler Language="C#" Class="ajax" %>

     using System;
     using System.Reflection;
     using System.Web;

     /**
     * Ajax.ashx
     * 
     * Enables client callback resource to client javascript.
     * Functions are relayed to DataManager for processing.
     */
     public class ajax : IHttpHandler 
     {
      /// <summary>
      /// Processes the client request
      /// </summary>
      public void ProcessRequest (HttpContext context) 
      {
        // Set the content type so the client's browser will handle the response correctly
        context.Response.ContentType = "text/json";

        // Get the function name from the request GET/POST parameters
        string function = context.Request["Function"];
   context.Response.Write(function);
}
    }    

数据管理器.cs/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.Web;

namespace GWR
{
  class DataManager: Util
  {

    #region Properties
    /// <summary>
    /// Accesses the Request object
    /// </summary>
    private static HttpRequest Context
    {
        get { return HttpContext.Current.Request; }
    }
    #endregion


    #region GetWeather
    public static string GetWeather()
    {
        return ToJson(new
        {
            //LoginSideHtml = RenderUserControl("LoginSide.ascx", null),
            //LoginMainHtml = RenderUserControl("LoginMain.ascx", null)
            AlertHTML = "<h1>Got Here Alert Weather</h1>",
            CurrentHTML = "<h2>Got here current weather",
            FiveDayHTML = "<h3>5 day forcast</h3>"

        });
    }

    #endregion

    #region ToJson
    /// <summary>
    /// Converts an object into JSON compatible form
    /// </summary>
    /// <param name="o"></param>
    /// <returns></returns>
    public static string ToJson(object o)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();

        return serializer.Serialize(o);
    }
    #endregion
  }

}

4

1 回答 1

2

这个:

data: $.extend({
        Function: "GetWeather"
    })

应该:

data: $.extend({},{
        "Function": "GetWeather"
     })

或者:

data: {"Function":"GetWeather"}

或者:

data: "Function=GetWeather"
于 2012-07-09T15:31:41.520 回答