我正在尝试将Postcode Anywhere实施到我的网站中

时间:2016-07-06 06:32:24

标签: javascript

我正在尝试将Postcode Anywhere实施到我的网站中。因此,如果他们在事件邮政编码字段中输入邮政编码,系统将显示与该邮政编码相关的地址,并且用户将选择相关地址,地址详细信息将然后填充事件地址字段的其余部分。我在我的页面中使用以下代码:

function CapturePlus_Interactive_Find_v2_10Begin(Key, SearchTerm, LastId, SearchFor, Country, LanguagePreference, MaxSuggestions, MaxResults) {
    var script = document.createElement("script"),
        head = document.getElementsByTagName("head")[0],
        url = "http://services.postcodeanywhere.co.uk/CapturePlus/Interactive/Find/v2.10/json3.ws?";

    // Build the query string
    url += "&Key=" + encodeURIComponent(Key);
    url += "&SearchTerm=" + encodeURIComponent(SearchTerm);
    url += "&LastId=" + encodeURIComponent(LastId);
    url += "&SearchFor=" + encodeURIComponent(SearchFor);
    url += "&Country=" + encodeURIComponent(Country);
    url += "&LanguagePreference=" + encodeURIComponent(LanguagePreference);
    url += "&MaxSuggestions=" + encodeURIComponent(MaxSuggestions);
    url += "&MaxResults=" + encodeURIComponent(MaxResults);
    url += "&callback=CapturePlus_Interactive_Find_v2_10End";

    script.src = url;

    // Make the request
    script.onload = script.onreadystatechange = function () {
        if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") {
            script.onload = script.onreadystatechange = null;
            if (head && script.parentNode)
                head.removeChild(script);
        }
    }

    head.insertBefore(script, head.firstChild);
}

function CapturePlus_Interactive_Find_v2_10End(response) {
    // Test for an error
    if (response.Items.length == 1 && typeof(response.Items[0].Error) != "undefined") {
        // Show the error message
        alert(response.Items[0].Description);
    }
    else {
        // Check if there were any items found
        if (response.Items.length == 0)
            alert("Sorry, there were no results");
        else {
            // PUT YOUR CODE HERE
            //FYI: The output is an array of key value pairs (e.g. response.Items[0].Id), the keys being:
            //Id
            //Text
            //Highlight
            //Cursor
            //Description
            //Next
        }
    }
}

我不确定在代码的else部分写什么(我需要把实际的代码映射到值)。有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

如果你看那里的网站,有一个有效的例子。查看页面背后的代码

http://www.pcapredict.com/support/webservice/captureplus/interactive/find/2.1/

祝你好运

function TestCallBack(response){
var _strHtml='';
var _intColumn=0;
var _intRow=0;
var _strColumns= new Array('Id','Text','Highlight','Cursor','Description','Next');
var _strColumn='';
//Hide the waiting panel
document.getElementById('btnTest').innerHTML = 'Start the test';
document.getElementById('btnTest').disabled = false;
document.getElementById('pnlOutput').style.display='block';
//Test for an error
if (response.length==1 && typeof(response[0].Error) != 'undefined'){
//Show the error message
_strHtml = '<table cellpadding="0" cellspacing="0" class="doc-table" style="width: 100%;">';
_strHtml += '<thead><tr><th><span class="cboxtl"></span>Error</th><th>Description</th><th>Cause</th><th><span class="cboxtr"></span>Resolution</th></tr></thead>';
_strHtml += '<tr><td>' + response[0].Error + '</td><td>' + response[0].Description + '</td><td>' + response[0].Cause + '</td><td>' + response[0].Resolution + '</td></tr>';
_strHtml += '</table>';
//Update the DIV
document.getElementById('pnlHeading').innerHTML = '<h2>Error</h2>';
document.getElementById('pnlError').innerHTML =  _strHtml;
document.getElementById('pnlError').style.display='block';
}
else{
//Check if there were any items found
if (response.length==0){
_strHtml = '<h2>Sorry, no matching items found</h2>';
}
else{
//Add the headings to the table
_strHtml += '<table cellpadding="0" cellspacing="0" class="doc-table" style="width: 100%; table-layout:auto;">';
_strHtml += '<thead><tr>';
for (_intColumn=0; _intColumn<_strColumns.length; _intColumn++)
{
_strHtml += '<th>' + _strColumns[_intColumn] + '</th>';
}
_strHtml += '</tr></thead>';
//Add the rows
for (_intRow=0; _intRow<response.length; _intRow++){
_strHtml += '<tr>'
if (response[_intRow].Id=='') { _strHtml += '<td>&nbsp;</td>' } else  { _strHtml += '<td>' + response[_intRow].Id + '</td>' };
if (response[_intRow].Text=='') { _strHtml += '<td>&nbsp;</td>' } else  { _strHtml += '<td>' + response[_intRow].Text + '</td>' };
if (response[_intRow].Highlight=='') { _strHtml += '<td>&nbsp;</td>' } else  { _strHtml += '<td>' + response[_intRow].Highlight + '</td>' };
if (response[_intRow].Cursor=='') { _strHtml += '<td>&nbsp;</td>' } else  { _strHtml += '<td>' + response[_intRow].Cursor + '</td>' };
if (response[_intRow].Description=='') { _strHtml += '<td>&nbsp;</td>' } else  { _strHtml += '<td>' + response[_intRow].Description + '</td>' };
if (response[_intRow].Next=='') { _strHtml += '<td>&nbsp;</td>' } else  { _strHtml += '<td>' + response[_intRow].Next + '</td>' };
_strHtml += '</tr>'
}
_strHtml += '</table>'
}
//Update the DIV
document.getElementById('pnlHeading').innerHTML = '<h3>Results</h3>';
document.getElementById('pnlResults').innerHTML = _strHtml;
document.getElementById('pnlResults').style.display='block';
document.getElementById('pnlError').style.display='none';
}
}