前台:
$("#getJsonpByJquery").click(function () {
$.ajax({
url: 'http://localhost:2701/home/somejsonp',
dataType: "jsonp",
jsonp: "callback",//jsonp默认回调参数为callback
success: function (data) {
console.log(data)
}
})
})
$.get('http://localhost:2701/home/somejsonp', { act: "searchType", "str": str }, function (data) {
if (data && data.PTSchema) {
if (data.PTSchema.length > 0) {
html.push('<ul>');
$.each(data.PTSchema, function (i, item) {
html.push("<li data-ptid='" + item.Id + "'>");
html.push("<a onclick='PTFullNameOnClick(\"" + item.Id + "\",\"" + item.FulllName + "\",\"\")'>");
html.push(item.FulllName);
html.push("</a>");
html.push("<\/li>");
});
}
}},"jsonp")//jsonp默认回调参数为callback
后台:
public override void ProcessRequest(HttpContext context)
{
context.Response.Write(searchType(dict["str"], dict["callback"]));
}
private string searchType(string s, string callback)
{
string str = callback + "()";
s = HttpUtility.UrlDecode(s);
if (ptModels.Count > 0)
{
return callback + "(" + Json.ToJson<PTSchema>(ptModels) + ")";
}
else
{
return str;
}
}