首页 文章

从jQuery到flutter / dart - $ .ajax

提问于
浏览
-3

我是新的扑动/飞镖,并想知道如何将这个js转换为颤动/飞镖?

我看过很多颤音/飞镖教程,但都是如何使用小部件渲染东西,我还没有找到任何关于“基础”类如何工作的教程 .

function callAjax(method, url, params, callback) {
		var asynchronously = true;
		if(callback === undefined) {
			asynchronously = false;
		}
		if(params === undefined) {
			params = '';
		}
		
		var ajaxUrl = url;
		ajaxUrl = ajaxUrl.toLowerCase() + params;
		
		var result = null;
		
		$.ajax(ajaxUrl, {
			processData: false,
			async: asynchronously,
			data: {},
			type: method,
			contentType: 'application/json',

			success:function(jsondata){
				if(asynchronously) { 
					callback(jsondata); 
				} else {
					result = jsondata;
				}
			},
				
			error:function(xhr, status, error){
				alert('Internal Server Error: ' + xhr.status + ' (' + url + params + ')');
				if(asynchronously) { callback(null); }
			},
		});	
		
		if(!asynchronously) { return result; }
	}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>

到目前为止......

import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;

class CallAjax {
  String method;
  String url;
  String params;
  Function callback;

  CallAjax(this.method, this.url, this.params, this.callback);
  
}

1 回答

  • 0

    我想我现在得到了:)发布代码以防其他人需要它 .

    Future<void> fetchHttpGet(String url, String params, Function callback) async {
      final response = await http
          .get(Uri.encodeFull(url + params),
        headers: {
          'Content-type' : 'application/json',
          'Accept': 'application/json'
        },
      );
    
      if(response == null || response.statusCode != 200) {
        callback(null);
      } else {
        final responseJson = json.decode(response.body);
        callback(responseJson);
      }
    }
    

相关问题