Solution
Cross-domain post requests are not advocated.
Cross-domain solution for Ajax in 0.jquery Jsonp
. ashx Code
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 clas s Get:ihttphandler {public void ProcessRequest (HttpContext context) {s
Etresponsecontext (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 page
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");
}
);