在JS中如何实现ajax和同源策略

在JS中如何实现ajax和同源策略


守着星空守着你
浏览 746回答 1
1回答

慕标琳琳

<!DOCTYPE&nbsp;html>&nbsp;<html&nbsp;lang="en"><head>&nbsp;<meta&nbsp;charset="UTF-8">&nbsp;<title>Title</title>&nbsp;<script&nbsp;src="{%&nbsp;static&nbsp;'JS/jquery-3.1.1.js'&nbsp;%}"></script></head><body>&nbsp;<button&nbsp;class="send_Ajax">send_Ajax</button>&nbsp;<script>&nbsp;//$.ajax的两种使用方式:&nbsp;&nbsp;//$.ajax(settings);&nbsp;//$.ajax(url,[settings]);&nbsp;&nbsp;&nbsp;$(".send_Ajax").click(function(){&nbsp;&nbsp;&nbsp;$.ajax({&nbsp;&nbsp;url:"/handle_Ajax/",&nbsp;&nbsp;type:"POST",&nbsp;&nbsp;data:{username:"Yuan",password:123},&nbsp;&nbsp;&nbsp;success:function(data){&nbsp;&nbsp;&nbsp;alert(data)&nbsp;&nbsp;},&nbsp;&nbsp;&nbsp;&nbsp;//===================&nbsp;error============&nbsp;&nbsp;&nbsp;error:&nbsp;function&nbsp;(jqXHR,&nbsp;textStatus,&nbsp;err)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;jqXHR:&nbsp;jQuery增强的xhr&nbsp;&nbsp;&nbsp;//&nbsp;textStatus:&nbsp;请求完成状态&nbsp;&nbsp;&nbsp;//&nbsp;err:&nbsp;底层通过throw抛出的异常对象,值与错误类型有关&nbsp;&nbsp;&nbsp;console.log(arguments);&nbsp;&nbsp;&nbsp;},&nbsp;&nbsp;&nbsp;&nbsp;//===================&nbsp;complete============&nbsp;&nbsp;&nbsp;complete:&nbsp;function&nbsp;(jqXHR,&nbsp;textStatus)&nbsp;{&nbsp;&nbsp;&nbsp;//&nbsp;jqXHR:&nbsp;jQuery增强的xhr&nbsp;&nbsp;&nbsp;//&nbsp;textStatus:&nbsp;请求完成状态&nbsp;success&nbsp;|&nbsp;error&nbsp;&nbsp;&nbsp;console.log('statusCode:&nbsp;%d,&nbsp;statusText:&nbsp;%s',&nbsp;jqXHR.status,&nbsp;jqXHR.statusText);&nbsp;&nbsp;&nbsp;console.log('textStatus:&nbsp;%s',&nbsp;textStatus);&nbsp;&nbsp;},&nbsp;&nbsp;&nbsp;//===================&nbsp;statusCode============&nbsp;&nbsp;statusCode:&nbsp;{&nbsp;&nbsp;&nbsp;'403':&nbsp;function&nbsp;(jqXHR,&nbsp;textStatus,&nbsp;err)&nbsp;{&nbsp;&nbsp;&nbsp;console.log(arguments);&nbsp;//注意:后端模拟errror方式:HttpResponse.status_code=500&nbsp;&nbsp;&nbsp;},&nbsp;&nbsp;&nbsp;&nbsp;'400':&nbsp;function&nbsp;()&nbsp;{&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;})&nbsp;&nbsp;})&nbsp;</script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript