我试图发出一个简单的Ajax请求,以便在控制器中可以从db中获取数据并返回它。现在,我在视图上有以下代码:
id
此代码在控制器上
void callbackDispatcher() {
Workmanager.executeTask((task, inputData){
try {
var connectivityResult = await (Connectivity().checkConnectivity());
final result = await InternetAddress.lookup('google.com');
if (connectivityResult == ConnectivityResult.mobile) {
// I am connected to a mobile network.
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
print('connected');
}
} else if (connectivityResult == ConnectivityResult.wifi) {
// I am connected to a wifi network.
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
print('connected');
}
}
} on SocketException catch (_) {
print('not connected');
}
return Future.value(true);
});
}
void main() {
WidgetsFlutterBinding.ensureInitialized();
Workmanager.initialize(
callbackDispatcher, // The top level function, aka callbackDispatcher
isInDebugMode:
false // If enabled it will post a notification whenever the task is running. Handy for debugging tasks
);
// Periodic task registration
Workmanager.registerPeriodicTask("2", "syncfirebase",
// When no frequency is provided the default 15 minutes is set.
// Minimum frequency is 15 min. Android will automatically change your frequency to 15 min if you have configured a lower frequency.
frequency: Duration(minutes: 15),
initialDelay: Duration(seconds: 10),
constraints: Constraints(networkType: NetworkType.connected));
runApp(new MyApp());
}
如何在视图上回显变量$ enc的每一行?
答案 0 :(得分:2)
首先,您不需要json_encode()
$result
变量,因为Laravel会为您处理。您目前的代码从技术上讲意味着您将需要再次解码数据。
第二,您实际上不需要使用response()->json()
,因为从路由/控制器方法返回数组将始终自动转换为JSON。比起您实际需要,这更像是仅供参考。
将您的控制器方法更改为:
public function ajaxRequestPost(Request $request)
{
return [
'success' => 'Got Simple Ajax Request.',
'lots' => DB::select('select * from users'),
];
}
然后遍历结果,您可以使用for
循环:
success: function (data) {
for (var i = 0; i < data.lots.length; i++) {
console.log(data.lots[i]);
}
}
或者由于您使用的是jQuery $.each(...):
success: function (data) {
$.each(data.lots, function (index, item) {
console.log(item);
});
}