Javascript cross-origin requests encountered when developing web applications using ASP. NET

Source: Internet
Author: User
Tags tojson

The solution does not advocate cross-origin post requests. 0. cross-origin scheme jsonp of ajax in jquery. 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 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 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") ;}}) ;}jsonp only supports GET requests and does not support POST requests, even if you write a POST, it will automatically convert it to a GET request and put your data in querystring. 1. Modify the web. config file. The entire application supports cross-origin requests. Web. config <system. webServer>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.