简体   繁体   中英

XMLHttpRequest JavaScript not running after Open Method

I have a problem with my javascript function, I notice that the Http Post Request was not arriving at my server.. So I inserted a few alert boxes on my javascript code to see where was the problem..

Here is my javascript function:

function callService(id) {

    id.innerHTML = "Clicked!";
    alert("Before do XMLHttpRequest!");
    var xmlhttp = new XMLHttpRequest();

    alert("Before do url!");
    var url = "http://this_is_an_address_valid_but_i_wont_show_you/";

    alert("Before do open!");


    xmlhttp.open("POST", url, true);

    alert("Before do setRequestHeader!");
    xmlhttp.setRequestHeader("Content-type", "application/json");

    alert("Before do onreadystatechange!");
    xmlhttp.onreadystatechange = function () { //Call a function when the state changes.
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            alert(xmlhttp.responseText);
        }
    }

    alert("Before do parameters!");
    var parameters = JSON.stringify({"Values": {"Value": 2500,"ItemNumber": "1"},"PartnerID": "S","ProdCode": "C","TC": "111","OpCode": "10"});

    alert("Before do send!");
    xmlhttp.send(parameters);
    alert("After do send!!");

}

I notice that I don't see the alert box "Before do setRequestHeader!" , so I guess the open method of the XMLHttpRequest is not working?

Thanks alot in advance, can someone help me?

You have a cross domain issue: check this answer: How to make cross domain request .

In a few words, you can't make an AJAX call to a server if the JS file has been obtained from another one (security reasons).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM