Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Json未定义错误 我有要编辑的操作,然后发送json数据_Php_Jquery_Json_Phalcon - Fatal编程技术网

Php Json未定义错误 我有要编辑的操作,然后发送json数据

Php Json未定义错误 我有要编辑的操作,然后发送json数据,php,jquery,json,phalcon,Php,Jquery,Json,Phalcon,我想输入该有效值,但当我发出警报时,它仅显示undefined BtnEdit : function(val){ var form=$('#edit_salary'); $.ajax({ type:'POST', data: form.serialize(), dataType:'json', url : "btnedit", success:function(d){

我想输入该有效值,但当我发出警报时,它仅显示undefined

BtnEdit : function(val){
    var form=$('#edit_salary');
    $.ajax({
        type:'POST',
        data: form.serialize(),
        dataType:'json',
        url : "btnedit",
        success:function(d){
            alert(d.valid);      //undefined
            alert(d);            //{"valid":true}                     
        }
    });
}
用于变换对象中的字符串:

success:function(d){
    d = JSON.parse(d);
    alert(d.valid);      // true
}

您需要使用
JSON.parse
来转换对象中的字符串:

success:function(d){
    d = JSON.parse(d);
    alert(d.valid);      // true
}
试一试


由于您正在使用Phalcon框架,我建议您使用它的内置功能来正确处理json响应:

$this->response->setContentType('application/json', 'UTF-8');
$this->response->setJsonContent($responseAsArray);
$this->response->send();
如果响应中包含
application/json
的头,jQuery将开始正确处理它,您不需要
json.parse
您的帧

为了在我的一个应用程序中处理API模块中的json响应,我有一个
ControllerBase
,它使用此事件处理程序扩展了
\Phalcon\Mvc\Controller

use \Phalcon\Mvc\Controller;

class ControllerBase extends Controller {

/**
 * Captures method result and tries to make a JSON response out of it.
 * 
 * @param \Phalcon\Mvc\Dispatcher $dispatcher
 * @return \Phalcon\Http\Response
 */
protected function afterExecuteRoute($dispatcher) {

    $content = $dispatcher->getReturnedValue();

    if(is_object($content)) {
        if(is_callable(array($content, 'toArray'))) {
            $content = $content->toArray();
        } else {
            $content = (array) $content;
        }
    }

    $frame = $this->getFrame($content, $dispatcher); // protocol frame creation helper

    $this->response->setContentType('application/json', 'UTF-8');
    switch($frame['code']) {
        case 200:
            $this->response->setStatusCode(200, 'OK');
            break;
        case 400: 
            $this->response->setStatusCode(404, 'Not found');
            break;
        case 500: 
            $this->response->setStatusCode(503, 'Service Unavailable');
            break;
    }

    // clearing potential warnings
    $ob = ob_get_clean();
    if(strlen($ob) > 0) {
        /**
         * @todo some logging of $ob !
         * this will be a dead code if you will make an Error2Exception handler
         */
        echo($ob); die();
    }

    // settinf response content as JSON
    $this->response->setJsonContent($frame);

    return $this->response->send();
}
}

首先解析
JSON
。您已经谈到了JSON.Parse,但您没有在示例中使用它???@AsimShahzad该示例在我的回答中的
下。当然我可以看到它,但请使用您所说的@Hassan方法
$this->response->setContentType('application/json', 'UTF-8');
$this->response->setJsonContent($responseAsArray);
$this->response->send();
use \Phalcon\Mvc\Controller;

class ControllerBase extends Controller {

/**
 * Captures method result and tries to make a JSON response out of it.
 * 
 * @param \Phalcon\Mvc\Dispatcher $dispatcher
 * @return \Phalcon\Http\Response
 */
protected function afterExecuteRoute($dispatcher) {

    $content = $dispatcher->getReturnedValue();

    if(is_object($content)) {
        if(is_callable(array($content, 'toArray'))) {
            $content = $content->toArray();
        } else {
            $content = (array) $content;
        }
    }

    $frame = $this->getFrame($content, $dispatcher); // protocol frame creation helper

    $this->response->setContentType('application/json', 'UTF-8');
    switch($frame['code']) {
        case 200:
            $this->response->setStatusCode(200, 'OK');
            break;
        case 400: 
            $this->response->setStatusCode(404, 'Not found');
            break;
        case 500: 
            $this->response->setStatusCode(503, 'Service Unavailable');
            break;
    }

    // clearing potential warnings
    $ob = ob_get_clean();
    if(strlen($ob) > 0) {
        /**
         * @todo some logging of $ob !
         * this will be a dead code if you will make an Error2Exception handler
         */
        echo($ob); die();
    }

    // settinf response content as JSON
    $this->response->setJsonContent($frame);

    return $this->response->send();
}
}