C# MVC 實現 ajax 跨域
js
$(function() {
$.ajax({
url: "http://localhost:1266/test/t",
data: { "a": 1, "b":2, "c": 3 },
dataType: "jsonp",
jsonp: "callback", //Jquery生成驗證參數的名稱
timeout: 3000,
crossDomain: true,
error: function (a, b, c) {
if ("timeout" == c) {
alert("超時");
} else {
console.log(c);
}
},
success: function (json) {
alert(json.code+",a="+json.a);
}
});
});
html:
@{
ViewBag.Title = "測試頁";
}
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<body>
嘿嘿嘿
</body>
<script src="~/Comtent/Js/dataup.js"></script>
//控制器
public class TestController : BaseController
{
/// <summary>
/// 跨域測試
/// </summary>
/// <returns></returns>
public ActionResult T()
{
var a = Request["a"];
var b = Request["a"];
var c = Request["a"];
return Content(Request["callback"] + "(" + JsonConvert.SerializeObject(new { code = 0, a = a, b = b, c = c }) + ")");
}}
C# MVC 實現 ajax 跨域