1.
1 @{2Layout =NULL;3 }4 5<! DOCTYPE html>6 789<meta name="Viewport"Content="Width=device-width"/>Ten<TITLE>JS cross-domain calls </title> One<script src="~/scripts/jquery-1.8.2.min.js"></script> A<script> - $ (function () { - $.ajax ({ theType"GET",//must be get mode -DataType:'Jsonp',//The data type must be JSONP -Jsonp'Jsonp_callback', -Url:'http://localhost:14373/test/GetString?callback=?',//must have a callback=? Name can be changed + success:function (DA) { -$("#name"). HTML (da.name); +$("#value"). HTML (da.value); A }, Error:function () { atAlert"ERROR"); - } - }); - }); -</script> - in<body> -<span id="name"></span> to<span id="value"></span> +</body> -HTML code2.
1 usingSystem;2 usingSYSTEM.WEB.MVC;3 4 namespaceMVC4. Controllers5 {6 Public classTestcontroller:controller7 {8 Publicactionresult Index ()9 {Ten returnView (); One } A - Public voidGetString () - { the varResponse =Httpcontext.response; -Response. ContentType ="Text/json"; - stringstr = request.querystring["Callback"];//JS Accept Variable name -Response. Write (str +"({\ "name\":"+"\""+"name"+"\""+", \ "value\":"+"\""+"value"+"\"})");//Return Data + } - } +}
Controller CodeC#JS cross-domain calls