0

My partner and I are trying to get a domain that I own, communicate with a ios app that is run on objective c to work via http. He is using the code that was provided by this link Sending an HTTP POST request on iOS.

He is able to do a GET to receive the data in my .txt page but when he performs a PUT to try and write to that file so that I can get that data it fails. We are both rather new to http so it is possible that we are missing something. A concern we have is that he doesn't have the privileges to write to this file. Any advice would help, thanks!

Here is the javascript I am using on my side. I added a header to my response to try and resolve the cors issue.

(function () {
window.onload = function () {
    httpGetAsync("http://students.washington.edu/bharatis/distances.txt", processData)
    //alert("hello inside onload");
    document.getElementById("first").innerHTML = leader1;
    document.getElementById("second").innerHTML = leader1;
    document.getElementById("third").innerHTML = leader1;
    //window.onbeforeunload = update;
}

function processData(responseText) {
    //alert(responseText);
    var txt = "";
    var x = responseText.getElementsByTagName('Distance'); // Talk to alex about
    for(i = 0; i < x.length; i++) {
        txt += x[i].childNodes[0].nodeValue;
    }

    var result = parseDouble(txt);
    alert(result);

}

function httpGetAsync(theUrl, callback) {
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.onreadystatechange = function() { 
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
            callback(xmlHttp.responseText);
        }
    xmlHttp.open("GET", theUrl, true); // true for asynchronous 
    xmlHttp.setRequestHeader("Access-Control-Allow-Origin", "*");
    xmlHttp.send("response message");
}

})();

sach
  • 7
  • 4
  • Is there any more information you can provide about the failure? Status code, etc? – Garrett Johnson Jun 03 '18 at 23:12
  • All the response says is "he requested method PUT is not allowed for the URL" – sach Jun 03 '18 at 23:29
  • Is it something to do with the fact the request doesn't come from the same origin? – sach Jun 03 '18 at 23:30
  • This may be a CORS issue (same origin you mentioned), or it may simply be that the server is not configured with a route coming from a POST request to the URL you are attempting to contact. As Garrett mentioned, without seeing code it is nearly impossible to guess the correct problem or solution. Help us help you. – Randy Casburn Jun 03 '18 at 23:44
  • I will update the main post! – sach Jun 04 '18 at 00:18
  • The Access-Control-Allow-Origin header needs to be sent by the `http://students.washington.edu/bharatis/distances.txt ` server. It’s a response header, not a request header. So you also need to remove the `xmlHttp.setRequestHeader("Access-Control-Allow-Origin", "*")` from your request code. – sideshowbarker Jun 04 '18 at 04:05
  • It's also possible that the server isn't set up to handle a PUT request but is set up for GET. That's what the message sounds like, at least – Garrett Johnson Jun 08 '18 at 22:07

0 Answers0