0

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?

TiagoM
  • 3,458
  • 4
  • 42
  • 83
  • 1
    I suggest you to replace `alert()` functions by `console.log()`, and check the console log (just pressing F12 in your browser) to check any error messages – Pablo Lozano May 21 '14 at 15:06
  • But what I will see on the console, will be the logs I insert using "console.log()" , the behaviour it's like using alert() . Console won't show me any other errors right ? – TiagoM May 21 '14 at 15:12
  • Thanks for your help, I notice on the console now that there is a error on the method open, it says "Permission Denied" can you help me ? – TiagoM May 21 '14 at 15:52
  • If a line of code is not being executed, surely there is an error before that line, and the error message will appear in the console with red letters – Pablo Lozano May 21 '14 at 15:52
  • I don't experience the problem you describe when I [copy/paste the code](http://jsbin.com/rexusazu/1/edit) and add a call to the function. – Quentin May 21 '14 at 16:14
  • The URL is http://localhost/bla/blah . And the html file is running on a specific server with a specific domain server.. – TiagoM May 21 '14 at 16:24

1 Answers1

-2

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).

Community
  • 1
  • 1
Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59
  • 1
    If there was a cross domain issue, it wouldn't show up until the response was received. It certainly wouldn't show up before the `send()` method was called. – Quentin May 21 '14 at 16:12
  • I don´t agree, while open(): http://stackoverflow.com/questions/5793831/script5-access-is-denied-in-ie9-on-xmlhttprequest – Pablo Lozano May 21 '14 at 20:37