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条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥20 怎么用php获取ip地址和js读取php数组的参数
      • ¥60 iPad应用程序UPV打开Windows系统相同应用下的模型文件
      • ¥15 MySQL行转动态列的问题
      • ¥15 通过STM32F4实现智能清洁小车,要求用到三个以上传感器
      • ¥15 perl代码解释说明
      • ¥15 列举 五阶幻方 有多少种
      • ¥15 请教一下能人在VS中调用MATLAB绘制动态驻波
      • ¥15 VB.NET如何调用Microsoft Excel?(WPS存在时)
      • ¥15 KUKA机器人EtherNetIP通讯配置教程
      • ¥15 VB6.0如何模拟键盘组合按键