首页 > 解决方案 > 我可以在本机反应中使用 jquery 吗?

问题描述

我可以在反应式本机应用程序中使用 ajax 吗?如果不是,我如何使用 fetch 执行类似的命令?

        $.ajax({
            type: "GET",
            url: "https://test.com/api",
            contentType: "application/json",
            crossDomain: true,
            dataType: "text",
            xhrFields: {
                withCredentials: true
            },
            username: 'user',
            password: 'pass',
            failure: function(errMsg) {
                alert(errMsg);
            }
        }).done(function(data) {
            console.log(data)

        });

标签: jqueryajaxreact-nativeget

解决方案


这里有一个用于获取数据的示例获取 API 等等

return fetch(
      "URL",
      {
        headers: {
          "Accept": "application/json",
          "Authorization": "Basic " + btoa('username:password'),
          "Content-Type": "application/json",
          "User-Agent":
            "Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25",
          "x-correlation-id": "asfsef4243sdf",
          "x-channel": "android"
        },
        method: "GET", // *GET, POST, PUT, DELETE, etc.
      }
    )
      .then(response => {
        console.log("got json" + response);
        response.json();
      })
      .then(responseJson => {
        console.log("Hey = "+responseJson);
      })
      .catch(error => {
        console.error(error);
      });

推荐阅读