I am trying to pass a JSON object that looks similar to this:
我试图传递一个类似于这个的JSON对象:
{"service": "AAS1", "sizeTypes":[{"id":"20HU", "value":"1.0"},{"id":"40FB","2.5"}]}
Just a note: In the sizeTypes, there are a total of about 58 items in the array.
请注意:在sizeTypes中,数组中总共有大约58个项目。
When the user clicks the submit button, I need to be able to send the object to a PHP script to run an UPDATE query. Here is the javascript that should be sending the JSON to the PHP script:
当用户单击提交按钮时,我需要能够将对象发送到PHP脚本以运行UPDATE查询。这是应该将JSON发送到PHP脚本的javascript:
$('#addNewSubmit').click(function()
{
var payload = {
name: $('#addservice').val();
sizeTypes: []
};
$('input.size_types[type=text]').each(function(){
payload.sizeTypes.push({
id: $(this).attr('id'),
value: $(this).val()
});
});
$.ajax({
type: 'POST',
url: 'api/editService.php',
data: {service: payload},
dataType: 'json',
success: function(msh){
console.log('success');
},
error: function(msg){
console.log('fail');
}
});
});
Using the above click function, I am trying to send the object over to php script below, which is in api/editService.php:
使用上面的click函数,我试图将对象发送到下面的php脚本,该脚本位于api / editService.php中:
<?php
if(isset($_POST['service']))
{
$json = json_decode($_POST['service'], true);
echo $json["service"]["name"] . "<br />";
foreach ($json["service"]["sizeTypes"] as $key => $value){
echo $value["value"] . "<br />";
}
}
else
{
echo "Nooooooob";
}
?>
I do not have the UPDATE query in place yet because I am not even sure if I am passing the JSON correctly. In the javascript click function, you see the SUCCESS and ERROR functions. All I am producing is the ERROR function in Chrome's console.
我还没有UPDATE查询,因为我甚至不确定我是否正确传递了JSON。在javascript单击功能中,您可以看到SUCCESS和ERROR功能。我正在制作的是Chrome控制台中的ERROR功能。
I am not sure where the error lies...in the javascript or the php.
我不确定错误在哪里...在javascript或php中。
I wasn't sure if I needed to display the FORM where the object is coming from. If necessary, please let me know and I will update the question.
我不确定是否需要显示对象来自的FORM。如有需要,请告诉我,我会更新问题。
For the time being, please help me figure out why I can only produce the error function in the AJAX post.
暂时,请帮我弄清楚为什么我只能在AJAX帖子中产生错误功能。
Thank you very much.
非常感谢你。
**** EDIT ****
****编辑****
I removed the dataType in the ajax call, and added JSON.stringify to data:
我删除了ajax调用中的dataType,并将JSON.stringify添加到数据中:
$.ajax({
type: 'POST',
url: 'api/editService.php',
data: {servce: JSON.stringify(payload)},
success: function(msg){
console.log('success');
},
error: function(msg){
console.log('fail'), msg);
}
});
In the PHP script, I tried this:
在PHP脚本中,我试过这个:
if(isset($_POST['service'))
{
$json = json_decode($_POST['service'], true);
foreach ($json["service"]["sizeTypes"] as $key => $value){
$insert = mysqli_query($dbc, "INSERT INTO table (COLUMN, COLUMN, COLUMN) VALUES (".$json["service"] . ", " . "$value["id"] . ", " . $value["value"]")");
}
}
else
{
echo "noooooob";
}
With this update, I am able to get the success message to fire, but that's pretty much it. I cannot get the query to run.
通过此更新,我可以获得成功消息,但这就是它。我无法让查询运行。
Any thoughts?
有什么想法吗?
4 个解决方案
#1
2
without seeing the error, I suspect the error is because ajax is expecting json (dataType: 'json',) but you are echoing html in your php
没有看到错误,我怀疑错误是因为ajax期待json(dataType:'json',)但你在php中回显html
#2
1
Try to change
试着改变
error: function(msg){
console.log('fail');
}
to
至
error: function(msg){
console.log(msg);
}
There might be some php error or syntax issue and you should be able to see it there.
可能有一些PHP错误或语法问题,你应该能够在那里看到它。
Also try to debug your php script step by step by adding something like
还尝试通过添加类似的东西逐步调试您的PHP脚本
echo "still works";die;
回声“仍然有效”;死亡;
on the beginning of php script and moving it down till it'll cause error, then you'll know where the error is.
在PHP脚本的开头并将其向下移动直到它将导致错误,然后您将知道错误在哪里。
Also if you're expecting JSON (and you are - dataType: 'json'
in js , don't echo any HTML in your php.
此外,如果您期待JSON(并且您在js中是 - dataType:'json',请不要在您的php中回显任何HTML。
#3
1
As you are sending an object in your service
key, you probably have a multi-dimensional array in $_POST['service']
.
当您在服务密钥中发送对象时,您可能在$ _POST ['service']中有一个多维数组。
If you want to send a string, you should convert the object to json:
如果要发送字符串,则应将对象转换为json:
data: {service: JSON.stringify(payload)},
Now you can decode it like you are doing in php.
现在你可以像在php中一样解码它。
Also note that you can only send json back from php if you set the dataType
to json
. Anything other than valid json will have you end up in the error
handler.
另请注意,如果将dataType设置为json,则只能从php发回json。除了有效的json以外的任何东西都会让你最终进入错误处理程序。
#4
0
Example how to handle a JSON
response from editService.php
. Typically, the editService.php
script will be the worker and will handle whatever it is you need done. It will (typically) send a simple response back to the success
method (consider updating your $.ajax
to use the latest methods, eg. $.done
, etc). From there you handle the responses appropriately.
示例如何处理来自editService.php的JSON响应。通常,editService.php脚本将是worker,并将处理您需要完成的任何操作。它(通常)会将简单响应发送回success方法(考虑更新$ .ajax以使用最新方法,例如$ .done等)。从那里你可以适当地处理答案。
$.ajax({
method: 'POST',
url: '/api/editService.php',
data: { service: payload },
dataType: 'json'
})
.done(function(msh) {
if (msh.success) {
console.log('success');
}
else {
console.log('failed');
}
})
.fail(function(msg) {
console.log('fail');
});
Example /editService.php
and how to work with JSON
via $.ajax
示例/editService.php以及如何通过$ .ajax使用JSON
<?php
$response = [];
if ( isset($_POST['service']) ) {
// do your stuff; DO NOT output (echo) anything here, this is simply logic
// ... do some more stuff
// if everything has satisfied, send response back
$response['success'] = true;
// else, if this logic fails, send that response back
$response['success'] = false;
}
else {
// initial condition failed
$response['success'] = false;
}
echo json_encode($response);
#1
2
without seeing the error, I suspect the error is because ajax is expecting json (dataType: 'json',) but you are echoing html in your php
没有看到错误,我怀疑错误是因为ajax期待json(dataType:'json',)但你在php中回显html
#2
1
Try to change
试着改变
error: function(msg){
console.log('fail');
}
to
至
error: function(msg){
console.log(msg);
}
There might be some php error or syntax issue and you should be able to see it there.
可能有一些PHP错误或语法问题,你应该能够在那里看到它。
Also try to debug your php script step by step by adding something like
还尝试通过添加类似的东西逐步调试您的PHP脚本
echo "still works";die;
回声“仍然有效”;死亡;
on the beginning of php script and moving it down till it'll cause error, then you'll know where the error is.
在PHP脚本的开头并将其向下移动直到它将导致错误,然后您将知道错误在哪里。
Also if you're expecting JSON (and you are - dataType: 'json'
in js , don't echo any HTML in your php.
此外,如果您期待JSON(并且您在js中是 - dataType:'json',请不要在您的php中回显任何HTML。
#3
1
As you are sending an object in your service
key, you probably have a multi-dimensional array in $_POST['service']
.
当您在服务密钥中发送对象时,您可能在$ _POST ['service']中有一个多维数组。
If you want to send a string, you should convert the object to json:
如果要发送字符串,则应将对象转换为json:
data: {service: JSON.stringify(payload)},
Now you can decode it like you are doing in php.
现在你可以像在php中一样解码它。
Also note that you can only send json back from php if you set the dataType
to json
. Anything other than valid json will have you end up in the error
handler.
另请注意,如果将dataType设置为json,则只能从php发回json。除了有效的json以外的任何东西都会让你最终进入错误处理程序。
#4
0
Example how to handle a JSON
response from editService.php
. Typically, the editService.php
script will be the worker and will handle whatever it is you need done. It will (typically) send a simple response back to the success
method (consider updating your $.ajax
to use the latest methods, eg. $.done
, etc). From there you handle the responses appropriately.
示例如何处理来自editService.php的JSON响应。通常,editService.php脚本将是worker,并将处理您需要完成的任何操作。它(通常)会将简单响应发送回success方法(考虑更新$ .ajax以使用最新方法,例如$ .done等)。从那里你可以适当地处理答案。
$.ajax({
method: 'POST',
url: '/api/editService.php',
data: { service: payload },
dataType: 'json'
})
.done(function(msh) {
if (msh.success) {
console.log('success');
}
else {
console.log('failed');
}
})
.fail(function(msg) {
console.log('fail');
});
Example /editService.php
and how to work with JSON
via $.ajax
示例/editService.php以及如何通过$ .ajax使用JSON
<?php
$response = [];
if ( isset($_POST['service']) ) {
// do your stuff; DO NOT output (echo) anything here, this is simply logic
// ... do some more stuff
// if everything has satisfied, send response back
$response['success'] = true;
// else, if this logic fails, send that response back
$response['success'] = false;
}
else {
// initial condition failed
$response['success'] = false;
}
echo json_encode($response);