大家帮忙看看,这个AJAX的SUCCESS方法怎么没收到JS文件呢?
$.ajax({
url:"/ware_subject/GetPs",
data:"",
type:"post",
datatype:"text",
success:function(data){
alert("success");
},
error:function()
{
alert("error");
}
});
controller:
public void GetPs()
{
response.wirtefile("/Scripts/aa.js");
}aa.js:
alert("nihao");结果是。aa.js 直接在页面执行了,AJAX。的SUCCESS没有收到数据呢?
如何让aa.js不直接执行,而是传到AJAX的SUCCESS的方法里面。应该怎么写呢?
$.ajax({
url:"/ware_subject/GetPs",
data:"",
type:"post",
datatype:"text",
success:function(data){
alert("success");
},
error:function()
{
alert("error");
}
});
controller:
public void GetPs()
{
response.wirtefile("/Scripts/aa.js");
}aa.js:
alert("nihao");结果是。aa.js 直接在页面执行了,AJAX。的SUCCESS没有收到数据呢?
如何让aa.js不直接执行,而是传到AJAX的SUCCESS的方法里面。应该怎么写呢?
{
private string _Script;
private Dictionary<string, AjaxMethodInfo> _AjaxMethods;
private List<PropertyInfo> _AjaxProperties;
/// <summary>
/// 利用反射,创建js脚本
/// </summary>
/// <param name="type"></param>
public AjaxClass(Type type)
{
List<string> scripts = new List<string>();
Dictionary<string, AjaxMethodInfo> methodList = new Dictionary<string, AjaxMethodInfo>();
List<PropertyInfo> propertyList = new List<PropertyInfo>(); MethodInfo[] methods;
PropertyInfo[] properties;
string clientName;
object[] attrs = type.GetCustomAttributes(typeof(AjaxAttribute), false);
if (attrs.Length > 0)
{
AjaxAttribute attr = attrs[0] as AjaxAttribute;
clientName = string.IsNullOrEmpty(attr.Name) ? type.Name : attr.Name;
if (attr.Inherited)
{
methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);
properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty);
}
else
{
methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.DeclaredOnly);
}
}
else
{
clientName = type.Name;
methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.DeclaredOnly);
} foreach (MethodInfo mi in methods)
{
attrs = mi.GetCustomAttributes(typeof(AjaxAttribute), false);
if (attrs.Length > 0)
{
AjaxReturnType returnType = AjaxReturnType.Html;
if (mi.ReturnType == typeof(int) || mi.ReturnType == typeof(System.Web.Mvc.JsonResult))
returnType = AjaxReturnType.Json;
//AjaxReturnType returnType = (attrs[0] as AjaxAttribute).ReturnType;
AjaxMethodInfo ami = new AjaxMethodInfo(mi, returnType);
methodList.Add(mi.Name, ami);
scripts.Add(BuildAjaxRequest(ami));
}
}
foreach (PropertyInfo pi in properties)
{
attrs = pi.GetCustomAttributes(typeof(AjaxAttribute), false);
if (attrs != null && attrs.Length > 0) propertyList.Add(pi);
} if (methodList.Count > 0) _AjaxMethods = methodList;
if (propertyList.Count > 0) _AjaxProperties = propertyList; BuildScript(clientName, scripts, propertyList);
}
/// <summary>
/// 输入所有属性的js脚本
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public string GetScript(object obj)
{
if (_AjaxProperties == null) return _Script; string script = string.Empty;
foreach (PropertyInfo pi in _AjaxProperties)
{
if (script == string.Empty) script = BuildAjaxObject(obj, pi);
else script += ",\r\n " + BuildAjaxObject(obj, pi);
} return _Script.Replace("{property}", script);
}
/// <summary>
/// 创建最终的js脚本
/// </summary>
/// <param name="typeName"></param>
/// <param name="scripts"></param>
/// <param name="propertyList"></param>
private void BuildScript(string typeName, List<string> scripts, List<PropertyInfo> propertyList)
{
if (scripts.Count > 0 || propertyList.Count > 0)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine();
sb.AppendLine(" <script type=\"text/javascript\">");
sb.AppendFormat(" var {0} = {{", typeName);
if (propertyList.Count > 0)
{
sb.AppendLine();
sb.Append(" {property}");
}
for (int i = 0; i < scripts.Count; i++)
{
if (i == 0 && propertyList.Count == 0) sb.AppendLine();
else sb.AppendLine(",");
sb.Append(" " + scripts);
}
sb.AppendLine();
sb.AppendLine(" }");
sb.AppendLine(" </script>"); _Script = sb.ToString();
}
}
/// <summary>
/// jquery 相关ajax方法的脚本构建
/// </summary>
/// <param name="ami"></param>
/// <returns></returns>
private string BuildAjaxRequest(AjaxMethodInfo ami)
{
string methodName = ami.MethodInfo.Name;
string url = "{url}" + methodName + "{querystring}";
ParameterInfo[] parameters = ami.MethodInfo.GetParameters();
AjaxReturnType returnType = ami.ReturnType;
string param, data; if (parameters == null || parameters.Length == 0)
{
param = "callback";
data = string.Empty;
}
if (parameters.Length == 0)
{
return string.Format(@"{0}: function(callback)
{{
$.getJSON('{1}', callback);
}}",
methodName, url);
}
else
{
string[] paramArray = new string[parameters.Length + 1];
string[] dataArray = new string[parameters.Length];
for (int i = 0; i < parameters.Length; i++)
{
paramArray = parameters.Name;
dataArray = string.Format("{0}:{0}", parameters.Name);
}
//paramArray[parameters.Length] = "callback"; param = string.Join(", ", paramArray);
param = param.Trim ().TrimEnd(',');
data = string.Join(", ", dataArray);
} return string.Format(@"{0}: function({1},callback)
{{
$.getJSON('{2}',{{{3}}}, callback);
}}",
methodName,param, url,data);
} private string BuildAjaxObject(object obj, PropertyInfo pi)
{
object value = pi.GetValue(obj, null);
object[] attrs = pi.GetCustomAttributes(typeof(AjaxAttribute), false);
if (attrs.Length > 0)
{
AjaxAttribute attr = attrs[0] as AjaxAttribute;
if (attr.ReturnType == AjaxReturnType.Json && value is string)
return string.Format(@"{0}: {1}", pi.Name, value);
} StringBuilder sb = new StringBuilder();
JsonWriter jw = new JsonWriter(sb);
jw.Write(value);
return string.Format(@"{0}: {1}", pi.Name, sb.ToString());
}
}
{
protected override void Initialize(RequestContext requestContext)
{
base.Initialize(requestContext); InitJavaScript();
} /// <summary>
/// 初始化 Ajax使用的JavaScript
/// </summary>
private void InitJavaScript()
{
string jsb = AjaxManager.GetAjaxScript(this);
string controller = Convert.ToString(this.RouteData.Values["controller"]);
if (!string.IsNullOrEmpty(jsb) && !string.IsNullOrEmpty(controller))
{
string str = System.Web.HttpContext.Current.Request.ApplicationPath;
str = str.Length > 1 ? str : string.Empty;
jsb = jsb.Replace("{url}", string.Format("{0}/{1}/", str, controller));
jsb = jsb.Replace("{querystring}", GetHttpQueryString());
}
ViewData["m_javablock"] = jsb;
}
private string GetHttpQueryString()
{
StringBuilder sb = new StringBuilder("?");
foreach (KeyValuePair<string, object> pair in this.RouteData.Values)
{
if (pair.Key != "controller" && pair.Key != "action")
sb.AppendFormat("{0}={1}&", pair.Key, (pair.Value!=null)?pair.Value.ToString():"");
}
sb.Append(System.Web.HttpContext.Current.Request.QueryString.ToString());
return sb.ToString();
}
}
public class AjaxManager
{
public static string GetAjaxScript(object obj)
{
AjaxClass ajaxClass = new AjaxClass(obj.GetType());
return ajaxClass.GetScript(obj);
}
}
[Ajax]
public AjaxResult TestMVC(int i, int j)
{
int I = 0;
List<student> list = new List<student>();
for (int k = 0; k < 10; k++)
{
student sd = new student() { sname = "aaa" + k.ToString() + j.ToString(), ID = k, Grade = k * 10 };
list.Add(sd);
}
var stu = (from m in list
where m.ID == i
select m
).FirstOrDefault(); return new AjaxResult(true, stu);
} <script type="text/javascript">
var HomeController = {
TestMVC: function(i, j,callback)
{
$.getJSON('/Home/TestMVC?id=&',{i:i, j:j}, callback);
}
}
</script>HomeController.TestMVC(j,j+1, function(data) {
$("#divStudent").html(data.value.sname);
});参见
http://www.pin&&5i.com/showtopic-26968-2.html
我在网上找了很久都没找到相关的知识!
网上操作XML的就比较多,我就想把JS放到XML文件中操作应该比较方便吧?
File.ReadAllText("")字符串
Controller里面,转到一个View