简体   繁体   中英

sending email via XMLHttpRequest no response

I tried to manage editing the template but I'm still not able to send email, I'm creating an subscription box, here's my code so far:

var x= document.getElementById("emailtxt").value;

var uploadFormData = new FormData();
uploadFormData.append("email", x);

var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://realfashionstreet.com/Emailme.php',true);
xhr.onload=function() {
    alert(this.status);
    if(this.status==200) {
        console.log('data sent');
    alert(xhr.responseText);
    } else {
        //alert(this.status);
        alert('Something Went Wrong, Try Again Later!');
    }
};

/*xhr.addEventListener("load", function () {
  document.getElementById('skm_LockPane').className= 'LockOff';
}, false);*/

xhr.send(uploadFormData);
return false;

}

Here's the box I'm getting : http://realfashionstreet.com

It's just not showing any error or any response and also not sending email, but I think that site template doesn't allow to use php files if anyone have any other idea on how to send email using javascript…

Thanks

You can't for security reasons. See the same origin policy for JavaScript.

There are some workarounds that exploit browser bugs or corner cases, but using them is not recommended.

The best approach is having a server-side proxy that receives Ajax requests, and in turn, sends HTTP requests to other servers. This should be carefully implemented by sanitizing input and whitelisting the types of requests that are sent, and the servers that are contacted.

You're missing the www. in the request, this is causing a cross domain policy violation. This is the error I see in the console.

XMLHttpRequest cannot load http://realfashionstreet.com/Emailme.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.realfashionstreet.com' is therefore not allowed access. Default.asp:1

Ensure the URLs for the page and the XHR request have the exact same domains. Including subdomains like www

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