我有一个两部分的问题。
- 我如何让我的脚本提醒变量
type
在javascript代替[object object]
? - 我如何让我的Json成功转换为数组而不返回
NULL
?
$(document).ready(function() {
// Arrays
var title = $('.table-item-1 > strong').map(function () {
var value = $(this).text();
return value;
}).get();
var name = $('[width|="23%"]').next('.table-item-1').map(function () {
var value = $(this).text();
return value;
}).get();
var phone = $('[width|="23%"]').next('.table-item-1').next('.table-item-1').map(function () {
var value = $(this).text();
return value;
}).get();
var fax = $('[width|="23%"]').next('.table-item-1').next('.table-item-1').next('.table-item-1').map(function () {
var value = $(this).text();
return value;
}).get();
var email = $('[width|="23%"]').next('.table-item-1').next('.table-item-1').next('.table-item-1').next('.table-item-1 , a').map(function () {
var value = $(this).text();
return value;
}).get();
// Individual
var brand = $('.panel-1 tr strong').html();
var corp = $('.panel-1 tr td :gt(0)').html();
var web = $('.panel-1 tr td ~ .focus :not(strong)').html();
var lna_code = $('.panel-1 tr td :last').html();
var lna_class = $('.panel-1 tr td :gt(4)').html();
var items = {
'brand': brand,
'corp': corp,
'web': web,
'lna_code': lna_code,
'lna_class': lna_class,
'title': title,
'names':
name,
'phones':
phone,
'faxes':
fax,
'emails':
email,
};
var data = $.toJSON(items);
$.ajax({
type: 'POST',
url: 'http://xxxxxx.com/xxxxx/PHP/excel.php',
data: {'data':data},
success: function(data) {
alert('success');
},
complete: function(data){
alert(data); // alerts [object object] , want a string
}
});
});
PHP: <?php
$json = $_POST['data'];
$result = json_decode($json);
$resultsType = gettype($result);
echo $resultsType;
?>
注意:我使用json.js的$.toJSON(items);
调用
如果你想让结果在PHP端是一个数组,它应该在javascript端是一个数组:
[value1]
不是{key1 = value1}
在你的代码中发生的是你创建了一个对象。对象可能是你真正想要的,但它似乎不像你必须迭代的值在PHP端,这将是一个真正的数组,而不是一个对象的唯一原因。
如果您保持代码不变,则可以像这样访问PHP端的值:
echo $result->brand;