gpt4 book ai didi

javascript - 在 PHP 中调用 Javascript 函数

转载 作者:行者123 更新时间:2023-11-28 00:23:01 25 4
gpt4 key购买 nike

我需要调用 JavaScript 函数并使用结果(Json)作为 PHP 的变量。我有以下功能:

<script type="text/javascript" src="Scripts/jquery-1.9.1.min.js"></script>    
<script>
$(function () {

var baseUrl = 'https://XXXXXXXXX/';
var uid = 'XXXX';
var pwd = 'XXXX';


GetProd('DT.X0CEB.009', '1');

function GetProd(cod_prod, qtd) {
jQuery.support.cors = true;
$.ajax({
url: baseUrl + 'api/Prod?ref=' + encodeURIComponent(cod_prod) + '&qtd=' + qtd,
type: 'GET',
jsonp: "callback",
dataType: 'jsonp',
username: uid,
password: pwd,
success: function (data, textStatus, xhr) {
document.write(data);
},
error: function (xhr, textStatus, errorThrown) {
alert(xhr.responseText);
}
});
}
});
</script>

这将返回:

Object { Ref: "DT.X0CEB.009", Desc: "Monitor UltraHD P2715Q 68.6cm 27"", State: 0, Price: Object, Tax: Object, Stock: 2, Availability: null }

有人可以帮助我如何在 php 中使用这些字段(ref、desc、state、...)吗?问候佩德罗

最佳答案

您将它们作为 GET 参数发送,因此它们应该可以通过 $_GET 在您的端点中使用。您还可以尝试通过 POST 发送数据对象,并在 php API 中使用 $_POST 检索它们。

$.ajax({
url: 'yourUrl',
method: 'POST',
data: {
param1: param1,
param2: param2
},
success: function(data) {
//do something with data
}
});

PHP

$param1 = $_POST['param1'];
$param2 = $_POST['param2'];

关于javascript - 在 PHP 中调用 Javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29801846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com