如何访问特定的 API?

我正试图访问OpenFEMAAPI来为数据服务,但我对API非常陌生,而且我在用Javascript和CORS访问API的时候也遇到了同样的问题。

我的代码如下:

function createCORSRequest(method, url) {
    var xhr = new XMLHttpRequest();

    if ("withCredentials" in xhr) {
        // Check if the XMLHttpRequest object has a "withCredentials" property.
        // "withCredentials" only exists on XMLHTTPRequest2 objects.
            xhr.open(method, url, true);
        console.log('1');
    } else if (typeof XDomainRequest != "undefined") {
        // Otherwise, check if XDomainRequest.
        // XDomainRequest only exists in IE, and is IE's way of making CORS requests.
        xhr = new XDomainRequest();
        xhr.open(method, url);
        console.log('2');
    } else {
        // Otherwise, CORS is not supported by the browser.
        xhr = null;
        console.log('3');
    }
    return xhr;
}

var xhr = createCORSRequest('GET', "http://www.fema.gov/api/open/v1/PublicAssistanceFundedProjectsDetails");

alert(xhr.responseText);

但警报盒出现时却是空的,我真的不知道做错了什么......

The function you are using only creates the request; it doesn't send it or wait for results. You would still need to set up an onReadyStateChange handler, then call xhr.send(), to see the results.

More importantly, though, the API you are accessing does not permit cross-origin requests. It cannot be accessed from off-site through Javascript.