解决方案
不提倡跨域的post请求。
0.jquery中ajax的跨域方案jsonp
.ashx代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace KB.DSN.Web.API.Tokens { /// <summary> /// Summary description for Get /// </summary> public class Get : IHttpHandler { public void ProcessRequest(HttpContext context) { setresponsecontext(context); var token = KB.DSN.BusinessAccess.UniqueCommunicationCode.GenerateUniqueCommunicationCode(); var outputobject = new { Head = new Models.KBJsonHeadResponse(), Body = new { Token = token } }; var outputjsonstring = Newtonsoft.Json.JsonConvert.SerializeObject(outputobject); context.Response.Write(context.Request.QueryString["callback"]+"("+outputjsonstring+")"); } private void setresponsecontext(HttpContext context) { context.Response.ContentEncoding = System.Text.Encoding.UTF8; context.Response.ContentType = "application/json"; } public bool IsReusable { get { return false; } } } }
html页面
function getToken_jsonp(){ $.ajax({ url: "http://192.168.0.111/api/tokens/get.ashx", type: "get", dataType: "jsonp", jsonp: "callback", async: false, contentType: "application/json", success: function(data){ //alert("getToken success"); $("#token").text($.toJSON(data)); //console.log(data); }, error:function(){ alert("getToken fail"); } }); }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索jsonp
, context
, function
, response
, using
, system
, public
, Ajax跨域访问ASP.NET
, javascript跨域请求
javascript跨域问题
,以便于您获取更多的相关知识。
时间: 2024-11-08 19:04:07