我想将请求参数发送到其他域我已经知道跨脚本需要JsonP,并且我已经将JsonP与Jquery ajax一起使用了但我不知道如何使用XMLHttpRequest进行跨脚本编写以下代码是我的基本XMLHttpRequest代码。我想我需要修改xhr.setRequestHeader(),我必须添加解析代码请给我任何想法var xhr;function createXMLHttpRequest(){ if(window.AtiveXObject){ xhr = new ActiveXObject("Microsoft.XMLHTTP"); }else{ xhr = new XMLHttpRequest(); } var url = "http://www.helloword.com"; }function openRequest(){ createXMLHttpRequest(); xhr.onreadystatechange = getdata; xhr.open("POST",url,true); xhr.setRequestHeader("Content-Type",'application/x-www-form-urlencoded'); xhr.send(data); }function getdata(){ if(xhr.readyState==4){ if(xhr.status==200){ var txt = xhr.responseText; alert(txt); } } }
3 回答

长风秋雁
TA贡献1757条经验 获得超7个赞
function JsonpHttpRequest(url, callback) {
var e = document.createElement('script');
e.src = url;
document.body.appendChild(script); // fyi remove this element later /assign temp class ..then .remove it later
//insetead of this you may also create function with callback value and use it instead
window[callback] = (data) => {
console.log(data); // heres you data
}
}
// heres how to use
function HowTouse(params) {
JsonpHttpRequest("http://localhost:50702/api/values/Getm?num=19&callback=www", "www")
}
添加回答
举报
0/150
提交
取消