Detailed explanation of JS cross-domain POST implementation steps

php中世界最好的语言
Release: 2018-05-12 11:18:13
Original
2224 people have browsed it

This time I will bring you a detailed explanation of the steps to implement JS cross-domain POST. What are theprecautionsfor JS cross-domain POST implementation? Here are practical cases, let’s take a look.

javascriptCross-domain is a very common problem, among which jsonp is the most commonly used method, but jsonp only supports get, not post, so if you want to post some data through jsonp , the head is big.

Post is implemented by generating a form in an iframe, and returns the value to the caller through postMessage.

In the first step, we first implement a back-end code that accepts jsonp. As for what language to use for implementation, you decide for yourself.

c#The code is:

protected void Page_Load(object sender, EventArgs e) { StringBuilder sbRet = new StringBuilder(); sbRet.Append(""); Response.Write(sbRet.ToString()); Response.End(); }
Copy after login

For example, what you want to return to me is { userName:'user1', password:'pass1' } , when I call http://localhost/test When ?jsoncallback=callme

you actually return.

The second step is to build a post test page in the local folder, such as d:\test.html

     
Copy after login

The third step is to browse and click submit to see if the returned value is < ;script>callme({ userName:'user1', password:'pass1' })means there is no problem with the back-end program.

The fourth step, we write a general code to implement the above html.

    测试一哈 
Copy after login

The fifth step, security issues,

window.onmessage = function(e) { //可通过 e 来判断来源,并做一些安全方面的处理,此处读者自己去研究吧,可以加个 console.log(e) 看看 e 有哪些内容。 if (callback) callback(e.data); }
Copy after login

I believe you have read the case in this article After mastering the method, please pay attention to other related articles on the php Chinese website for more exciting content!

Recommended reading:

react-navigation usage summary (with code)

Common addition, deletion, modification and query operations of JS DOM elements Detailed explanation

The above is the detailed content of Detailed explanation of JS cross-domain POST implementation steps. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!