使用PHP将Javascript对象存储到MySQL
如何使用PHP将Javascript对象保存到MySQL? 现在我正在使用谷歌地图API,它会返回一个响应对象使用PHP将Javascript对象存储到MySQL,javascript,php,jquery,mysql,google-maps-api-3,Javascript,Php,Jquery,Mysql,Google Maps Api 3,如何使用PHP将Javascript对象保存到MySQL? 现在我正在使用谷歌地图API,它会返回一个响应对象 directionsService.route({ origin: document.getElementById('start').value, destination: document.getElementById('end').value, travelMode: 'BICYCLING' }, function(response, status) {
directionsService.route({
origin: document.getElementById('start').value,
destination: document.getElementById('end').value,
travelMode: 'BICYCLING'
}, function(response, status) {
if (status === 'OK') {
directionsDisplay.setDirections(response); //The Return Response
//This function call ajax below to send response Obj with a key to store to MySQL
storeRouteDB(storeLocations, response);
showSteps(response, markerArray, display, map);
} else {
window.alert('Directions request failed due to ' + status);
}
});
我使用Ajax将对象发送到后端php
$.ajax({
type: "GET",
url: 'http://localhost:8080/storedRouteDB.php',
async: false,
data: {key: key, value: value},
error: function(){
console.log("Error in Ajax");
},
success: function (data) {
result = data;
}
});
在我的php中,我做到了:
$key = $_REQUEST['key'];
$value = $_REQUEST['value'];
echo checkDB($key, $value);
function checkDB($key, $value) {
$dbServerName = "localhost";
$dbUserName="root";
$dbPassword="";
$dbName="myDB";
....
// Trying to zip the object to String and save it as char to MYSQL
$value_zip = serialize($value);
$sql = "INSERT INTO storedRoute (locations, objects) VALUES ('$key', '$value_zip')";
但我得到的结果是:
Uncaught TypeError: Cannot read property 'b' of undefined
at _.n.intersects (js?key=AIzaSyAbBYjTy8g4-dGYAl4_mHmWDVoWGEziq6c&callback=initMap:147)
at i (jquery.min.js:2)
at jt (jquery.min.js:2)
at jt (jquery.min.js:2)
at jt (jquery.min.js:2)
at jt (jquery.min.js:2)
at Object.<anonymous> (jquery.min.js:2)
at Function.each (jquery.min.js:2)
at jt (jquery.min.js:2)
at jt (jquery.min.js:2)
Uncaught TypeError:无法读取未定义的属性“b”
在交叉点处(js?key=AIzaSyAbBYjTy8g4-dGYAl4_mhmwdowgeziq6c&callback=initMap:147)
at i(jquery.min.js:2)
在jt(jquery.min.js:2)
在jt(jquery.min.js:2)
在jt(jquery.min.js:2)
在jt(jquery.min.js:2)
反对。(jquery.min.js:2)
在Function.each(jquery.min.js:2)
在jt(jquery.min.js:2)
在jt(jquery.min.js:2)
如何修复此错误?或者有没有其他更好的方法使用PHP将Javascript对象存储到MySQL?谢谢大家! 我通常会这样做:
$.ajax({
type: 'POST',
url: 'http://localhost:8080/storedRouteDB.php',
async: false,
data: 'data='+JSON.stringify({key: key, value: value}),
error: function(){
console.log("Error in Ajax");
},
success: function (data) {
result = data;
}
});
然后您可以在
$\u POST['data']
中访问该对象。也许你也想用json\u decode
来解码它。让我知道它是否有效。“然后您可以访问$\u请求['data']”中的对象。$\u请求['data']
这是错误的,因为$\u请求是使用GET、POST和COOKIE数据设置的。使用$\u获取['data']
而不是关闭$\u请求['data']
。。$\u请求
数组或多或少类似于一个“全局”数组,我倾向于不惜任何代价避免它。那么,我将更新我的答案,可能会删除GET
类型,并将其替换为POST
。谢谢,这将不能跨API版本移植。使用Directions API的应用程序受谷歌地图平台服务条款的约束。条款中规定,除非在条款中规定的有限条件下,否则不得预取、缓存、索引或存储任何内容。