【发布时间】:2016-07-28 01:51:50
【问题描述】:
关于这个主题有很多线程,但我找不到任何使用 PHP 的线程。我想将 json 对象传递给视图,稍后我将使用返回的 json 对象更新一个元素。 这是我的代码: 查看:
<input type="submit" class="button" name="insert" value="load"/>
<script>
jQuery(document).ready(function() {
var $ = jQuery;
var baseUrl = [location.protocol, '//', location.host, location.pathname].join('');
$('.button').click(function(){
var clickBtnValue = $(this).val();
var ajaxurl = baseUrl+"?action=load";
data = {'action': clickBtnValue};
$.post(ajaxurl, {}, function (result) {
alert(result);
});
});
});
</script>
而控制器是:
<?php
set_include_path(get_include_path().':../');
require_once('_inc/common.php');
$action = req('action');
if ($action == 'load') {
$result = parse_ini_file('test.ini');
$json = json_encode($result);
}
[更新] 在提供答案的代码之后,我现在得到一个 Json.parse 错误。所以我再次编辑了我的代码,但错误仍然存在,我在网上检查了我的 Json 是否是有效的 json 并且验证器上没有错误。
$result = parse_ini_file($config_file);
$json = json_encode(array($result),JSON_HEX_QUOT);
var_dump($json);
header('Content-Type: application/json');
查看
var request = $.ajax({
url: ajaxurl,
method: "POST",
data: {},
dataType: "json"
});
request.done(function( msg ) {console.log("d");});
request.fail(function( jqXHR, textStatus ) {console.log( "Request failed: " + textStatus );});
});
【问题讨论】: