Using JSONP to completely solve the Ajax cross-domain access cookie session scheme

Source: Internet
Author: User

Recently do the development to put the picture file on another server (another domain name), because such distributed storage, the site will open much faster. When I used Ajax to get pictures of a user on the image server, I had a problem, I couldn't get the information in the usual way, the cookie was null, and then I thought that JavaScript and IFrame could not be accessed by the browser for security reasons. Then we have to consider using other means to complete, after 2 days finally found a solution, I use JSONP to solve, please see.

JS Code:

  1. $.ajax ({

  2. Type: "Get",

  3. URL: "http://www.oicqzone.com/userogin.aspx?jsoncallback=?",//Must be used here? jsoncallback=, otherwise it cannot be obtained.

  4. Data: {"name": Name, "Pass": pass},//Here is the parameter, can not

  5. DataType: "Jsonp",//This must, if not you can never get information

  6. Success:function (JSON) {

  7. var obj = eval (JSON); This is also necessary because we return with the Jsonp object, so we don't need to

  8. Here is the decomposition of specific elements, can be set according to their own needs Jsonp object.

  9. Alert ("Success JSON Data:" + obj.usrname + "Usrid:" + Obj.usrid + "Rndpassword:" + Obj.rndpassword);

  10. }

  11. });

Asp. NET code:

    1. Note that JSON objects and values must be held with double quotes, or JS cannot get the object

    2. String Strjson = request.querystring["Jsoncallback"] +

    3. String. Format ({{\ "usrid\": \ "{1}\", \ "usrname\": \ "{0}\", \ "rndpassword\": \ "{2}\"}}) ",

    4. Accounts.UserEntity.UserId, Accounts.UserEntity.UserName, Accounts.UserEntity.Password);

    5. Context. Response.AddHeader ("Access-control-allow-origin", "*");

    6. Response.Write (Strjson);

Using JSONP to completely solve the Ajax cross-domain access cookie session scheme

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.