将参数添加到Javascript AJAX

时间:2017-03-19 10:24:05

标签: javascript jquery html ajax

这是我简单的硬编码版本。

$(document).ready(function(){
        $.support.cors = true;
        var this_id = '123456789123456';
              $.ajax({
                  url: 'https://thisservice/delivery/'+this_id,
                  type: "get",
                  dataType: "json",
                  data: { },
                  success: function(response){
                      console.log(response);
                      var html = '';
                      html += '<tr>';
                      html += '<th>Customer Name: </th><td>'+response.custName+'</td>';
                      html += '</tr>';
                      html += '<tr>';
                      html += '<th>Address Line1:</th><td>'+response.addrLine1+'</td>';
                      html += '</tr>';
                      html += '<tr>';
                      html += '<th>Address Line2:</th><td>'+response.addrLine2+'</td>';
                      html += '</tr>';
                      html += '<th>Address Line3:</th><td>'+response.addrLine3+'</td>';
                      html += '</tr>';
                      html += '<th>Address Line4:</th><td>'+response.addrLine4+'</td>';
                      html += '</tr>';
                      html += '<th>Address Line5:</th><td>'+response.addrLine5+'</td>';
                      html += '</tr>';
                      html += '<th>Address Line6:</th><td>'+response.addrLine6+'</td>';
                      html += '</tr>';
                      html += '<th>Customer PostCode:</th><td>'+response.custPostCode+'</td>';
                      html += '</tr>';

                      $('#theDelivery').append(html);

                  }

              })});  

上面的代码完全正常,但我现在正在努力将this_id作为url参数,所以当网页与有效的第16位数作为子字符串一起调用时,它将返回我的对象​​的内容试图从这个网络服务访问。

究竟是怎么做到的?我试图在下面的代码中这样做,但没有运气。

<script type="text/javascript">
function getthisId(this_id){
    $.support.cors = true;

    $.ajax({
        type: "get",
        url: 'https://thisservice/delivery/'+this_id,
        dataType: "json",
        cache: false,
        data: { this_id },
        success: function (response) {
            console.log(response);
            var html = '';
            html += '<tr>';
            html += '<th>Customer Name: </th><td>'+response.custName+'</td>';
            html += '</tr>';
            html += '<tr>';
            html += '<th>Address Line1:</th><td>'+response.addrLine1+'</td>';
            html += '</tr>';
            html += '<tr>';
            html += '<th>Address Line2:</th><td>'+response.addrLine2+'</td>';
            html += '</tr>';
            html += '<th>Address Line3:</th><td>'+response.addrLine3+'</td>';
            html += '</tr>';
            html += '<th>Address Line4:</th><td>'+response.addrLine4+'</td>';
            html += '</tr>';
            html += '<th>Address Line5:</th><td>'+response.addrLine5+'</td>';
            html += '</tr>';
            html += '<th>Address Line6:</th><td>'+response.addrLine6+'</td>';
            html += '</tr>';
            html += '<th>Customer PostCode:</th><td>'+response.custPostCode+'</td>';
            html += '</tr>';

            $('#theDelivery').append(html);

        }
    });
}

$(document).ready(function () {
    getthisId(this_id);
});

发生此错误:

Uncaught ReferenceError: this_id is not defined
at HTMLDocument.<anonymous> (1032987988503654:59)
at i (jquery-1.12.3.min.js:2)
at Object.fireWith [as resolveWith] (jquery-1.12.3.min.js:2)
at Function.ready (jquery-1.12.3.min.js:2)
at HTMLDocument.K (jquery-1.12.3.min.js:2)

我是非常新的,任何帮助都会很棒:)

2 个答案:

答案 0 :(得分:1)

服务器端期望名为parcel_id的参数中的值,因此您需要将其作为您提供给data的对象中的键,如下所示:

data: { parcel_id: this_id },

答案 1 :(得分:0)

你应该这样做

'https://thisservice/delivery/?id='+this_id