PHP将JSON返回jquery Ajax调用
,尽管您努力通过JQUERY的AJAX功能与PHP进行通信,但您仍在继续遇到“选择器激活”错误。此外,检索到的数据似乎不正确。 Let's delve into the issue and identify the potential cause.
PHP with JSON ReturnThe snippet provided below illustrates how to return JSON data in PHP:
header('Content-Type: application/json'); echo json_encode([ '返回'=> 1, 'MSG1'=>'消息发送正常,我们将尽快与您联系' ); exit;注意header的包含('content-type:application/json');在Echo之前要指定JSON Content类型。
JAVASCRIPT和AJAXheader('Content-Type: application/json');
echo json_encode([
'return' => 1,
'msg1' => 'Message sent OK, we will be in touch ASAP'
]);
exit;
仅在表单验证后才触发您的ajax调用,以避免不必要的服务器请求。输出不得格格式化。看来正在打印jQuery的XHR对象。
潜在的陷阱验证以下内容: 确保您的php脚本响应您的php script with php supert type type type(content-type-type:application/juss a applive/jisson)。 Datatype设置为“ JSON”。
检查脚本是否在服务器上成功执行,并且该PHP配置为处理JSON输出。[&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3