我正在开发一个SharePoint项目。我想使用JQuery从主页进行ajax调用。但是,当我拨打电话时,我得到“Array.prototype.slice:'this'不是JavaScript对象”错误。
以下是我正在使用的文件广告代码: 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 / TEMPLATYS / LAYOUTS / 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
}
}
答案 0 :(得分:2)
此:
data: $.extend({
Function: "GetWeather"
})
应该是:
data: $.extend({},{
"Function": "GetWeather"
})
或:
data: {"Function":"GetWeather"}
或:
data: "Function=GetWeather"