xhttp发布不起作用。未定义Var

时间:2019-07-01 13:17:44

标签: javascript jquery ajax xmlhttprequest

我尝试调用API服务,我无法直接使用js进行调用,但必须从服务器进行调用。因此,我以这种方式发出了xhttp请求:

var myKey = "xxxxxxxxxx";

var name = $('#Inputfield_pad_firstname').val();

var company = $('#Inputfield_company_name').val();
var street1 = $('#Inputfield_pad_address').val();
var street2 = "";
var city = $('#Inputfield_pad_city').val();
var state = "SV";
var zip = $('#Inputfield_pad_postcode').val();
var country = $('select[id=Inputfield_country_registration]').val().toUpperCase();
var phone = $('#Inputfield_pad_phone').val();
var email = $('#Inputfield_email').val();
var TotalValue = "10";
var length = "5";
var width = "5";
var height = "5";
var weight = "1";

var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4) {
        console.log( this.response );
        alert(this.response);
    } else{
        alert(this.response);
    }
};
xhttp.responseType = 'json';
xhttp.open("POST", 'https://www.xxxxxx.it/form/getRates.php', true);
xhttp.send(JSON.stringify({
            'Method' : 'GetRates',
            'Params' : {
            'apikey' : myKey,
            'to_address': {
                'name': name,
                'company': company,
                'street1': street1,
                'street2': street2,
                'city': city,
                'state': state,
                'zip': zip,
                'country': country,
                'phone': phone,
                'email': email
            },
            'from_address' : {
                'name': 'xxxxx',
                'company': 'xxxxx',
                'street1': 'xxxxx',
                'street2': '',
                'city': 'xxxxx',
                'state': 'IT',
                'zip': 'xxxxx',
                'country': 'IT',
                'phone': 'xxxxx',
                'email': 'marco@xxxxxx.it'
            },
            'parcels' : [
                {
                    'length': length,
                    'width': width,
                    'height': height,
                    'weight': weight
                }
            ],
            'Insurance': 0,
            'InsuranceCurrency': 'EUR',
            'CashOnDelivery': 0,
            'CashOnDeliveryCurrency': 'EUR',
            'ContentDescription': 'things',
            'TotalValue': TotalValue,
            'ShippingService': 'Standard'
        }
    })
);

如果我尝试使所有变量的console.log告诉我它们未定义。 name除外。

在警报中总是返回我[object Object]

我不知道错误在哪里。 我认为错误是在js文件内部。不管怎样,这是我的php文件:

header('Access-Control-Allow-Origin: *');
$raw = file_get_contents('php://input');
$req = json_decode($raw);
if ( empty( $req->Params ) or empty( $req->Params->apikey ) ) die;
$connection = curl_init();
curl_setopt($connection, CURLOPT_URL, 'https://www.xxxx.com/api');
curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($connection, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);
curl_setopt($connection, CURLOPT_REFERER, $_SERVER["HTTP_REFERER"]);
curl_setopt($connection, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($connection, CURLOPT_USERPWD, $req->Params->apikey . ":");
curl_setopt($connection, CURLOPT_POST, 1);
curl_setopt($connection, CURLOPT_POSTFIELDS, json_encode($req) );
curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
echo curl_exec($connection);
curl_close($connection);

如果我尝试进行console.log,则所有.val()都是正确的。

Ready states中的所有XMLHttpRequest都被调用,但请求以[object Object]完成。

我该怎么做才能了解正在发生的事情?

谢谢

0 个答案:

没有答案
相关问题