Making HTTP GET Requests in JavaScript


透過Javascript實做Http 呼叫Api
var xhr = new XMLHttpRequest(); xhr.open('GET', "http://localhost:25959/api/login?Acc=123123&Pw=AA", true); xhr.send(); xhr.onreadystatechange = processRequest; function processRequest(e) { if (xhr.readyState == 4 && xhr.status == 200) { var response = JSON.parse(xhr.responseText); console.log(response.Acc); } }

readyState定義


ValueStateDescription
0UNSENTThe open method hasn't been called yet
1OPENEDThe send method has been called
2HEADERS_RECEIVEDThe send method has been called and the HTTP request has returned the status and headers
3LOADINGThe HTTP request response is being downloaded
4DONEEverything has completed



文章:https://www.kirupa.com/html5/making_http_requests_js.htm

留言

這個網誌中的熱門文章

數位信封 Digital Envelope

SSL的運作原理