1

I am trying to send a json by the post method, but it seems that it is not sent correctly, because it skips the code error function and I do not know why.

This is my form code:

<form name="login" action="" method="post">
    <input type="text" id="email" placeholder="User">
    <input type="password" id="pass" placeholder="Password">
    <button type="submit" id="send" onclick="sendForm()">Enter</button>
    <div class="moreoptions">
        <div class="ico">
            <span class="glyphicon glyphicon-lock" aria-hidden="true"></span>
            <a href="#">I forgot my password</a>
        </div>
        <div class="ico">
            <span class="glyphicon glyphicon-save" aria-hidden="true"></span>
            <a href="#">Register</a>
        </div>
    </div>
</form>

this is my js code:

function sendForm(){
    var fpU = ROT47(document.forms[0].elements[0].value);
    var fpP = ROT47(document.forms[0].elements[1].value);


var objJSON = {
    pdf: "login",
    fpU: fpU,
    fpP: fpP,
    browserInfo: {
        appCodeName: navigator.appCodeName,
        appName: navigator.appName,
        appVersion: navigator.appVersion,
        cookieEnabled: navigator.cookieEnabled,
        language: navigator.language,
        platform: navigator.platform,
        userAgent: navigator.userAgent
    },
    datasite: {
            "@accountID": "2",
            "@siteID": "3"
    }
};

$.ajax({
    url: 'https://www.evstest.com/G3v1LastVersion/portal/portal_action.php',
    data: objJSON,
    method: 'post',
    async: false,
    dataType: 'application/json',
    success: function(response){
        alert("done");
    },
    error: function(error){
        alert("error");
    }
});
}

I am working locally, and the URL that I have to send the json by the post method is external, just the one I put in the example, I say it in case it has something to do with it because my code fails.

Always ends up leaving the alert message. Anyone know I'm doing wrong?

Greetings and thanks!!!

David
  • 212
  • 1
  • 7
  • Are you getting any messages from your console ? You may not have access to *https://www.evstest.com*, either because your localhost is running on *http* or *cors* header is not set on *https://www.evstest.com*. – DavidDomain Nov 08 '16 at 13:35
  • Appears this message in the console when changing **https** for **http**: Request from another blocked source: the same source policy prevents reading the remote resource at http://www.evstest.com/G3v1LastVersion/portal/portal_action.php (reason: missing CORS 'Access-Control-Allow-Origin' header) ). – David Nov 08 '16 at 13:40
  • 1
    There you have it. You are not allowed to access *https://www.evstest.com*, because cors headers are not set. – DavidDomain Nov 08 '16 at 13:45
  • And these cors headers do I have to configure them or is it the one that handles the evtests server which has to do it ?? – David Nov 08 '16 at 13:49
  • They have to be set on the server *https://www.evstest.com* is running on. – DavidDomain Nov 08 '16 at 13:52
  • De acuerdo gracias.!!! – David Nov 08 '16 at 13:55
  • 2
    Also your post body is not JSON, if you pass you object through `JSON.stringify` you'll get JSON – Musa Nov 08 '16 at 13:58
  • True, I saw it later but the same thing is still happening with the CORS message – David Nov 08 '16 at 14:04
  • Sure that's from the server ?? Or the one that has to add them is who sends the POST to receive the answer ?? – David Nov 08 '16 at 14:19
  • 2
    @Rodrypaladin Think about it that way. Are you the one allowing others to visit you at home, or can other people just create some sort of invitation and walk in and out your home, without you knowing about it? Makes sense? – DavidDomain Nov 08 '16 at 14:43
  • Very true, you're right, and talk to the server manager to change what is necessary so you can send the data. thank you very much again – David Nov 08 '16 at 16:03

0 Answers0