Babak Majdy
Babak Majdy

Reputation: 55

http request | method post | request blocked | js & php

error 1 (solved): Access to XMLHttpRequest at 'http://example.com/projects/saveBoors/' from origin
'http://www.tsetmc.com' has been blocked by CORS policy:
No 'Access-Control-Allow-Origin' header is present on the requested resource.

error 2: Failed to load resource: net::ERR_FAILED

hey guys, Do you know how I can fix this 2 error? I am sending a request from js to php with method POST and this is my returned errors in console of browser...

my js:

true == function() {

    var xhr = new XMLHttpRequest();
    xhr.open("POST", 'http://195.234.191.26:8080/projects/saveBoors/', true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    var data = "";

    data = data + "namad=" + (l18) + "&";
    data = data + "name=" + (l30) + "&";
    data = data + "NumberTransactions=" + (tno) + "&";
    data = data + "Turnover=" + (tvol) + "&";
    data = data + "valueTransactions=" + (tval) + "&";
    data = data + "yesterdayPrice=" + (py) + "&";
    data = data + "priceFirst=" + (pf) + "&";
    data = data + "minPrice=" + (pmin) + "&";
    data = data + "maxPrice=" + (pmax) + "&";
    data = data + "lastPrice=" + (pl) + "&";
    data = data + "diffPrice=" + (plc) + "&";
    data = data + "plp=" + (plp) + "&";
    data = data + "finalPrice=" + (pc) + "&";
    data = data + "diffFinalPrice=" + (pcc) + "&";
    data = data + "pcp=" + (pcp) + "&";
    data = data + "tmin=" + (tmin) + "&";
    data = data + "tmax=" + (tmax) + "&";
    data = data + "number=" + (z) + "&";
    data = data + "mv=" + (mv) + "&";
    data = data + "pd1=" + (pd1) + "&";
    data = data + "zd1=" + (zd1) + "&";
    data = data + "qd1=" + (qd1) + "&";
    data = data + "po1=" + (po1) + "&";
    data = data + "zo1=" + (zo1) + "&";
    data = data + "qo1=" + (qo1) + "&";
    data = data + "pd2=" + (pd2) + "&";
    data = data + "zd2=" + (zd2) + "&";
    data = data + "qd2=" + (qd2) + "&";
    data = data + "po2=" + (po2) + "&";
    data = data + "zo2=" + (zo2) + "&";
    data = data + "qo2=" + (qo2) + "&";
    data = data + "pd3=" + (pd3) + "&";
    data = data + "zd3=" + (zd3) + "&";
    data = data + "qd3=" + (qd3) + "&";
    data = data + "po3=" + (po3) + "&";
    data = data + "zo3=" + (zo3) + "&";
    data = data + "qo3=" + (qo3) + "&";
    data = data + "bvol=" + (bvol) + "&";
    data = data + "cs=" + (cs) + "&";
    data = data + "numberActualBuyers=" + (ct).Buy_CountI + "&";
    data = data + "numberLegalBuyers=" + (ct).Buy_CountN + "&";
    data = data + "numberActualSellers=" + (ct).Sell_CountI + "&";
    data = data + "numberLegalSellers=" + (ct).Sell_CountN + "&";
    data = data + "actualPurchaseVolume=" + (ct).Buy_I_Volume + "&";
    data = data + "LegalPurchaseVolume=" + (ct).Buy_N_Volume + "&";
    data = data + "realSalesVolume=" + (ct).Sell_I_Volume + "&";
    data = data + "legalSalesVolume=" + (ct).Sell_N_Volume;




    xhr.send(data);


    console.log(data);





    return true;
}()

and my php:

<?php
define('HOST', 'localhost');
define('DBNAME', 'Boors');
define('DBUSER', 'root');
define('DBPASS', '');
$con = new PDO('mysql:host=' . HOST . ';dbname=' . DBNAME, DBUSER, DBPASS);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$con->exec('set names utf8');

if ($_POST == null){
    exit;
}

$namad = $_POST['namad'];
$name = $_POST['name'];
$NumberTransactions = $_POST['NumberTransactions'];
$Turnover = $_POST['Turnover'];
$valueTransactions = $_POST['valueTransactions'];
$yesterdayPrice = $_POST['yesterdayPrice'];
$priceFirst = $_POST['priceFirst'];
$minPrice = $_POST['minPrice'];
$maxPrice = $_POST['maxPrice'];
$lastPrice = $_POST['lastPrice'];
$diffPrice = $_POST['diffPrice'];
$plp = $_POST['plp'];
$finalPrice = $_POST['finalPrice'];
$diffFinalPrice = $_POST['diffFinalPrice'];
$pcp = $_POST['pcp'];
$tmin = $_POST['tmin'];
$tmax = $_POST['tmax'];
$Number = $_POST['Number'];
$mv = $_POST['mv'];
$pd1 = $_POST['pd1'];
$zd1 = $_POST['zd1'];
$qd1 = $_POST['qd1'];
$po1 = $_POST['po1'];
$zo1 = $_POST['zo1'];
$qo1 = $_POST['qo1'];
$pd2 = $_POST['pd2'];
$zd2 = $_POST['zd2'];
$qd2 = $_POST['qd2'];
$po2 = $_POST['po2'];
$zo2 = $_POST['zo2'];
$qo2 = $_POST['qo2'];
$pd3 = $_POST['pd3'];
$zd3 = $_POST['zd3'];
$qd3 = $_POST['qd3'];
$po3 = $_POST['po3'];
$zo3 = $_POST['zo3'];
$qo3 = $_POST['qo3'];
$bvol = $_POST['bvol'];
$cs = $_POST['cs'];
$numberActualBuyers = $_POST['numberActualBuyers'];
$numberLegalBuyers = $_POST['numberLegalBuyers'];
$numberActualSellers = $_POST['numberActualSellers'];
$numberLegalSellers = $_POST['numberLegalSellers'];
$actualPurchaseVolume = $_POST['actualPurchaseVolume'];
$LegalPurchaseVolume = $_POST['LegalPurchaseVolume'];
$realSalesVolume = $_POST['realSalesVolume'];
$legalSalesVolume = $_POST['legalSalesVolume'];






$sql = "INSERT INTO `details`(`namad`, `name`, `NumberTransactions`, `Turnover`, `valueTransactions`, `yesterdayPrice`, `priceFirst`, `minPrice`, `maxPrice`, `lastPrice`, `diffPrice`, `plp`, `finalPrice`, `diffFinalPrice`, `pcp`, `tmin`, `tmax`, `Number`, `mv`, `pd1`, `zd1`, `qd1`, `po1`, `zo1`, `qo1`, `pd2`, `zd2`, `qd2`, `po2`, `zo2`, `qo2`, `pd3`, `zd3`, `qd3`, `po3`, `zo3`, `qo3`, `bvol`, `cs`, `numberActualBuyers`, `numberLegalBuyers`, `numberActualSellers`, `numberLegalSellers`, `actualPurchaseVolume`, `LegalPurchaseVolume`, `realSalesVolume`, `legalSalesVolume`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
$query = $con->prepare($sql);
$query->bindValue(1, $namad);
$query->bindValue(2, $name);
$query->bindValue(3, $NumberTransactions);
$query->bindValue(4, $Turnover);
$query->bindValue(5, $valueTransactions);
$query->bindValue(6, $yesterdayPrice);
$query->bindValue(7, $yesterdayPrice);
$query->bindValue(8, $minPrice);
$query->bindValue(9, $maxPrice);
$query->bindValue(10, $lastPrice);
$query->bindValue(11, $diffPrice);
$query->bindValue(12, $plp);
$query->bindValue(13, $finalPrice);
$query->bindValue(14, $diffFinalPrice);
$query->bindValue(15, $pcp);
$query->bindValue(16, $tmin);
$query->bindValue(17, $tmax);
$query->bindValue(18, $Number);
$query->bindValue(19, $mv);
$query->bindValue(20, $pd1);
$query->bindValue(21, $zd1);
$query->bindValue(22, $qd1);
$query->bindValue(23, $po1);
$query->bindValue(24, $zo1);
$query->bindValue(25, $qo1);
$query->bindValue(26, $pd2);
$query->bindValue(27, $zd2);
$query->bindValue(28, $qd2);
$query->bindValue(29, $po2);
$query->bindValue(30, $zo2);
$query->bindValue(31, $qo2);
$query->bindValue(32, $pd3);
$query->bindValue(33, $zd3);
$query->bindValue(34, $qd3);
$query->bindValue(35, $po3);
$query->bindValue(36, $zo3);
$query->bindValue(37, $qo3);
$query->bindValue(38, $bvol);
$query->bindValue(39, $cs);
$query->bindValue(40, $numberActualBuyers);
$query->bindValue(41, $numberLegalBuyers);
$query->bindValue(42, $numberActualSellers);
$query->bindValue(43, $numberLegalSellers);
$query->bindValue(44, $actualPurchaseVolume);
$query->bindValue(45, $LegalPurchaseVolume);
$query->bindValue(46, $realSalesVolume);
$query->bindValue(47, $legalSalesVolume );
$query->execute();


header("Access-Control-Allow-Origin: http://www.tsetmc.com");

?>

the values in () like (mv) are pre defined variables!

Upvotes: 1

Views: 371

Answers (1)

greggailly
greggailly

Reputation: 96

CORS errors happen when your frontend does not run on the same ip/domain as the backend. Here your php sees the request is coming from elsewhere and for security reasons will not be executed.

Most of the time you will want to run both on the same server. If for development purposes this is not the case you can always add the following line at the top of your php to explicitly allow all origin:

header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");

Upvotes: 1

Related Questions