ASP.NET AJAX在指定JSON时返回XML
这是我第一次在ASP.NET中使用AJAX,请允许我使用一些绳子 我有一个简单的AJAX请求,我希望返回一个JSON字符串,但ASP.NET一直将JSON字符串格式化为XML 这是我的密码ASP.NET AJAX在指定JSON时返回XML,asp.net,ajax,webmethod,Asp.net,Ajax,Webmethod,这是我第一次在ASP.NET中使用AJAX,请允许我使用一些绳子 我有一个简单的AJAX请求,我希望返回一个JSON字符串,但ASP.NET一直将JSON字符串格式化为XML 这是我的密码 [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string GetModels() { string response; using (var context = new myDataModel(
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetModels()
{
string response;
using (var context = new myDataModel())
{
var m = from model in context.ListModels select model;
var serializer = new JavaScriptSerializer();
response = serializer.Serialize(m);
}
return response;
}
这被调用,但返回一个包含我的JSON字符串的XML节点
public JsonResult GetModels()
{
//.........
return new JsonResult() { Data = new { result=response } };
}
这是我使用ASP.NET从Web服务返回JSON的方法
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void GetJSON()
{
DataTable dtMarkers = new DataTable();
dtMarkers.Load(<<STORED PROCEDURE>>.GetReader());
string[][] myArray = new string[dtMarkers.Rows.Count][];
int i = 0;
foreach (DataRow marker in dtMarkers.Rows)
{
myArray[i] = new string[] { marker["ID"].ToString(), marker["Title"].ToString(), marker["Blurb"].ToString(), marker["Url"].ToString(), marker["LongLatPoint"].ToString() };
i++;
}
JavaScriptSerializer js = new JavaScriptSerializer();
string strJSON = js.Serialize(myArray);
Context.Response.Clear();
Context.Response.ContentType = "application/json";
Context.Response.Flush();
Context.Response.Write(strJSON);
}
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public void GetJSON()
{
DataTable dtMarkers=新DataTable();
加载(.GetReader());
字符串[][]myArray=新字符串[dtMarkers.Rows.Count]];
int i=0;
foreach(dtMarkers.Rows中的数据行标记)
{
myArray[i]=新字符串[]{marker[“ID”].ToString(),marker[“Title”].ToString(),marker[“Blurb”].ToString(),marker[“Url”].ToString(),marker[“LongLatPoint”].ToString();
i++;
}
JavaScriptSerializer js=新的JavaScriptSerializer();
字符串strJSON=js.Serialize(myArray);
Context.Response.Clear();
Context.Response.ContentType=“应用程序/json”;
Context.Response.Flush();
Context.Response.Write(strJSON);
}
此特定示例从数据库读取Googlemaps指针,并将其返回到ASP.NET页面,以便在Google Maps实例上显示。感谢Bjarki和simon,我已经调试了该方法,并看到JavaScriptSerializer返回json字符串,所以我假设是webmethod添加xml数据?