简体   繁体   中英

Encode punctuation in API call

I'm making a request to a real estate API with JavaScript. I send a street address and it sends me back info about the property. It works fine for most addresses. However, it's not working for an address that has periods in the address (the street = US 395) and I'm wondering if that punctuation is throwing it off, and if so, how it can be properly encoded.

// grab address from html
function getAddress() {
    address = $('.js-pma-address').val();
    address = $.trim(address);
    ...
    data(address, locale);
}


// compile data for API query
function data(address, locale) {
    dataObj.Key = ...;
    dataObj.Address = address;
    dataObj.LastLine = locale.toString();
    ...
    compileRequest(dataObj);
}

// create url for API request
function compileRequest(dataObj) {
    var request = 'http://rc.api.sitexdata.com/sitexapi/sitexapi.asmx/AddressSearch?';
    request += $.param(dataObj);
    runQueries(request);
}

// run api query 
function runQueries(request) {
    $.ajax({
    url: 'lp/proxy.php',
    data: {requrl: request + '&reportType=187'},
    dataType: 'xml'
    })

Try using encodeURIComponent() ... If punctuation is the culprit, that should correct it.

// compile data for API query
function data(address, locale) {
    dataObj.Key = ...;
    dataObj.Address = encodeURIComponent(address);  // <-- Right here
    dataObj.LastLine = locale.toString();
    ...
    compileRequest(dataObj);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM