weixin_33738578 2015-11-26 16:09 采纳率: 0%
浏览 13

Ajax / PHP:传递变量

When I'm passing the two variables to my php I get Notice: Undefined index : name & Notice: Undefined index : parsed.

When I'm adding an echo for each variable, I get nothing.

Nothing is added in my database.

name = "myName";
parsed = someCode.toJSON();

parsed = JSON.stringify(parsed, null, '\t');
parsed = parsed.replace(/[
\t]+([\d\.e\-\[\]]+)/g, '$1');

$.ajax({
    url: 'js/function/scriptSave.php',
    type: 'POST',
    dataType: 'JSON',
    data: {name : name, parsed : parsed},
    success: function(result) {
        alert("Success");
    },
    error: function(err){ 
        alert('Error'+err);
    }
});

The Ajax always returns me the error case : Error [object Object]

When I'm replacing my variables with text, everything works:

data: {name : 'name', parsed : 'parsed'}

And when I remove the json variable only it works. I really don't understand what happens here..

[EDIT]

The variable containing the name is ok. But when I only put the variable containing the json, it doesn't work. But what is the problem with this variable ?

So far, I have tried:

    var obj = {}; obj.name = 'myName'; obj.parsed = someCode.toJSON();
    {data : JSON.stringify(obj)}
///////
    {'name' : name, 'parsed' : parsed}
///////
    {parsed : JSON.stringify(parsed)}

Here's my php:

<?php
if (!@mysql_connect('localhost', 'user', 'pwd')) {
    die("Error");
} else {
    mysql_select_db('database');
}

    $parsed = $_POST["parsed"];
    $name = $_POST["name"];

    mysql_query("INSERT INTO object(name, parsed) VALUES ('".$name."', '".$parsed."')");
?>

[EDIT 2]

When I'm changing dataType from JSON to TEXT the ajax is in success case, but nothing is inserted in DB...

When I'm looking in my devTools in chrome everything semm to be ok :

General

Request Method :POST
Status Code: 200 OK

Response Headers

Content-Type: text/html

Request Headers

Content-Type: application/x-www-form-urlencoded; charset=UTF-8

Form Data

name : myName
parsed : {"another" : "value"}

[EDIT 3]

The problem seems to come from the json size... When I'm excluding some fields, everything seems to work. The problem is that in my databse, my field which will contain my JSON, is a LongText... The JSON size doesn't have to be the problem..

  • 写回答

2条回答 默认 最新

  • weixin_33725807 2015-11-27 09:12
    关注

    obj should be send as string, when calling the Ajax POST method.

    So, combine the data into one Object and stringify it with JSON.stringify() method.

    var obj = {};
    obj.name = "your name";
    obj.parsed = { "another" : "value" };
    
    $.ajax({
        url: 'js/function/scriptSave.php',
        type: 'POST',
        dataType: 'JSON',
        data: JSON.stringify(obj),
        success: function(result) {
            alert("Success");
        },
        error: function(err){ 
            alert('Error'+err);
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大