最近工作用到了跨域请求,所以此文就有了,概念网上都有,就不细说了,直接来了。

看了一篇文章,说的是通过扩展让ASP.NET Web API支持JSONP,jsonp网上有很多的教程,js代码部分基本和网上的一致,但是有很多都没有服务端的代码,

我写一下我实现的方法,希望与博友共同进步。

服务端:

 using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace Jsonp.Controllers
{
public class JsonpGController : Controller
{
public ContentResult GetJson()
{
string callBack = Request["callback"];//接受一下回调函数 return Content(callBack + "(" + JsonConvert.SerializeObject(GetPerson()) + ")", "application/json");//构造回调函数
}
private Person GetPerson()
{
return new Person() { Age = , Name = "贺晓冬" };
}
}
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
}

客户端:

@{
Layout = null;
} <!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script>
$(document).ready(function () {
$('#btn').one('click', function () {
$.ajax({
type: 'GET',
url: 'http://localhost:54406/JsonpG/GetJson',
dataType: 'jsonp',
success: callBack
});
})
})
function callBack(data)
{
$('#dv').find('p:first').text(data.Name);
$('#dv').find('p:last').text(data.Age);
}
</script>
</head>
<body>
<input type="button" id="btn" value="点我" />
<div id="dv">
<p></p>
<p></p>
</div>
</body>
</html>

返回的数据:

利用jQuery获取数据,JSONP的更多相关文章

  1. 基于MVC4+EasyUI的Web开发框架经验总结(12)--利用Jquery处理数据交互的几种方式

    在基于MVC4+EasyUI的Web开发框架里面,大量采用了Jquery的方法,对数据进行请求或者提交,方便页面和服务器后端进行数据的交互处理.本文主要介绍利用Jquery处理数据交互的几种方式,包括 ...

  2. (转)基于MVC4+EasyUI的Web开发框架经验总结(12)--利用Jquery处理数据交互的几种方式

    http://www.cnblogs.com/wuhuacong/p/4085682.html 在基于MVC4+EasyUI的Web开发框架里面,大量采用了Jquery的方法,对数据进行请求或者提交, ...

  3. label标签利用jquery获取值得方式为$("#message").html()

    label标签利用jquery获取值的方式为$("#message").text(), 赋值的方式为:$("message").html("北京欢迎你 ...

  4. Jquery学习笔记:利用jquery获取select下拉框的值

    jquery不是特别熟练,每次使用不常用的就要百度,特地记录下来. 我的下拉框是: <div class="form-group"> <select class= ...

  5. 利用jQuery获取鼠标当前的坐标

    文字来源:http://www.smalluv.com/jquery_code_106.html jQuery获取当前鼠标坐标位置: <div id="testDiv"> ...

  6. 利用jQuery获取jsonp

    前端js代码: $.ajax({ url: 'http://localhost:8080/webApp/somejsonp', dataType: "jsonp", jsonp: ...

  7. 利用jquery获取html中被选中的input的值

    单个按钮 <div id="wrap"> <input type="radio" name="payMethod" val ...

  8. 利用反射获取数据列+emit生成属性+单例模式

    1:IDictionary<string,string > 可以存储数据,将拼接的sql可以存储到这里下次可以使用 定义自定义属性表和列 typeof(T).GetCustomAttrib ...

  9. 利用 jquery 获取某个元素下的所有图片并改变其属性

    HTML代码 <div id="mochu"> <p>内容....<./p> <p><img src="xxxx.p ...

随机推荐

  1. 模块化定义JS,让统一文件夹内相同的变量不冲突

    两种方法: 1.(function(){……编写代码……})()   //先声明一个函数,声明完后直接调用 2.!function(){……编写代码……}()

  2. php基础教程笔记

    php的环境搭建很简单,从网上下载wamp service 2.5,官方网址http://www.wampserver.com/,有32位和64位的,必须下载跟系统一致的版本,不然会出现奇怪的错误,这 ...

  3. 学习笔记--C#特性Attribute(一)

    这个框框好烦人啊,删不掉 一.背景 [serializable] public class Person(){} 这是我第一次看到特性(Attribute),那时我还不知道这是什么,怎么会有这种写法, ...

  4. hdu 1823 Luck and Love 二维线段树

    题目链接 很裸的题, 唯一需要注意的就是询问时给出的区间并不是l<r, 需要判断然后交换一下, WA了好多发... #include<bits/stdc++.h> using nam ...

  5. Linux下安装memcached

    Linux下安装memcached 1.运行memcached需要本文开头介绍的libevent库 $ sudo yum install libevent libevent-deve 2.下载安装me ...

  6. 宣布正式发布 Windows Azure Notification Hub,新增 SQL Server AlwaysOn 可用性组侦听器支持

    今天,我们非常高兴地宣布,针对使用 Windows Azure 的移动和企业开发人员推出一些新功能.这些新功能可以减少构建移动应用程序的开发时间和成本,并能帮助企业开发人员实现高可用性和全球业务连续性 ...

  7. C# 动态载入Dll

    1.新建測试dll及方法,用vs2010新建winform程序,详细代码例如以下: using System; using System.Collections.Generic; using Syst ...

  8. VS EF Error: Configuration Error extension=".edmx" type="System.Data.Entity.Design.AspNet.EntityDesignerBuildProvider"

    错误截图: Configuration Error :<add extension=".edmx" type="System.Data.Entity.Design. ...

  9. 传统web和mvc的区别

  10. UVa1584 Circular Sequence

    #include <stdio.h>#include <string.h> int less(char* str, size_t len, size_t p, size_t q ...