<?php
public function btneditAction() {
$data['id'] = $this->request->getPost('id');
$data['uname'] = $this->request->getPost('uname');
$data['basesalary'] = $this->request->getPost('basesalary');
$data['travelfee'] = $this->request->getPost('travelfee');
$data['overtime'] = $this->request->getPost('overtime');
$data['ssc_emp'] = $this->request->getPost('ssc_emp');
$data['ssc_comp'] = $this->request->getPost('ssc_comp');
$Salarydetail = new SalaryMaster();
$pont=$Salarydetail->btnedit($data);
echo json_encode($pont);
$this->view->disable();
}
?>
它在网络中显示就像这样
{"valid":true} //when true {"valid":false} //when false
我想要那个有效值,但是当我发出警报时,它只显示未定义的值?
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}
}
});
}
答案 0 :(得分:7)
使用JSON.parse
转换对象中的字符串:
success:function(d){
d = JSON.parse(d);
alert(d.valid); // true
}
答案 1 :(得分:1)
您需要使用JSON.parse
转换对象中的字符串:
尝试
success:function(d){
d = JSON.parse(d);
alert(d.valid);
}
答案 2 :(得分:1)
截至您使用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();
}
}