传递jQuery的变量使用JSON [英] Passing jquery variable with json

查看:70
本文介绍了传递jQuery的变量使用JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个jQuery字符串传递给我的CakePHP的控制器,但我不断收到一个错误说json_de code需要一个字符串,但传递的数组。

I am trying to pass a jquery string to my cakephp controller but I keep getting an error saying json_decode expects a string but is passed an array.

下面是$ C $下我的ajax:

Here is the code for my ajax:

var str = 'Data that i need';

    var jsonString = JSON.stringify(str);

    $.ajax({
        type: "POST",
        url: "http:/Configs/",
        cache: false,
        datatype: 'json',
        data: {data : jsonString},
        timeout: 100000,

        success: function(data) 
        {
            alert('Ok');
        }
    });

和我的控制器我尝试接受她的数据,像这样:

and in my controller im trying to recieve the data like so:

$value = json_decode($_POST['data']);

但即时得到上述错误任何想法说如何解决这将是十分感谢。

but im getting the error stated above any idea how to solve this would be great thanks.

推荐答案

您确定您使用的URL是否正确? 如果您使用的是CakePHP的,那么你为什么不一起工作 $这个 - >请求 - >数据

Are you sure the url you use is correct? If you are using CakePHP, then why don't you work with $this->request->data?

尝试使用调试你的code

Try to debug your code with

pr($_POST); exit;

pr($this->request); exit;

他们是怎么说的?

What do they say?

这篇关于传递jQuery的变量使用JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆