JQuery尝试进行Ajax调用时出错

JQuery Error Trying to Make Ajax Call

本文关键字:调用 出错 Ajax JQuery      更新时间:2023-09-26

我正在处理一个SharePoint项目。我想使用JQuery从主页进行ajax调用。然而,当我进行调用时,我会得到"Array.prototype.slice:'this'不是JavaScript对象"错误

以下是我正在使用的文件和代码:主页.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);
}
    }    

DataManager.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
  }

}

这:

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

应该是:

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

或:

data: {"Function":"GetWeather"}

或:

data: "Function=GetWeather"