使用JsonP的JavaScript XMLHttpRequest

我想将请求参数发送到其他域


我已经知道跨脚本需要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);

        }

    }   

}


MMMHUHU
浏览 895回答 3
3回答

长风秋雁

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 usefunction HowTouse(params) {    JsonpHttpRequest("http://localhost:50702/api/values/Getm?num=19&callback=www", "www")}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript