向Javascript-AJAX添加参数
这是我简单的硬编码版本向Javascript-AJAX添加参数,javascript,jquery,html,ajax,Javascript,Jquery,Html,Ajax,这是我简单的硬编码版本 $(document).ready(function(){ $.support.cors = true; var this_id = '123456789123456'; $.ajax({ url: 'https://thisservice/delivery/'+this_id, type: "get",
$(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);
}
})});
$(文档).ready(函数(){
$.support.cors=true;
var this_id='123456789123456';
$.ajax({
网址:'https://thisservice/delivery/"这个"id",,
键入:“获取”,
数据类型:“json”,
数据:{},
成功:功能(响应){
控制台日志(响应);
var html='';
html+='';
html+=“客户名称:”+响应.custName+”;
html+='';
html+='';
html+='地址行1:'+响应.addrLine1+'';
html+='';
html+='';
html+=“地址行2:”+响应。地址行2+”;
html+='';
html+='地址行3:'+响应.addrLine3+';
html+='';
html+=“地址行4:”+响应。地址行4+”;
html+='';
html+=“地址行5:”+响应。地址行5+”;
html+='';
html+='地址行6:'+response.addrLine6+'';
html+='';
html+=“客户邮政编码:”+响应。客户邮政编码+”;
html+='';
$(“#交付”).append(html);
}
})});
上面的代码工作得非常好,但是我现在正努力将此_id作为url参数,因此当调用网页时,将使用有效的16位数字作为子字符串,它将返回我试图从此Web服务访问的对象的内容
到底是怎么做的?我在下面的代码中尝试过这样做,但没有成功
<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);
});
函数getthisId(此\u id){
$.support.cors=true;
$.ajax({
键入:“获取”,
网址:'https://thisservice/delivery/"这个"id",,
数据类型:“json”,
cache:false,
数据:{this_id},
成功:功能(响应){
控制台日志(响应);
var html='';
html+='';
html+=“客户名称:”+响应.custName+”;
html+='';
html+='';
html+='地址行1:'+响应.addrLine1+'';
html+='';
html+='';
html+=“地址行2:”+响应。地址行2+”;
html+='';
html+='地址行3:'+响应.addrLine3+';
html+='';
html+=“地址行4:”+响应。地址行4+”;
html+='';
html+=“地址行5:”+响应。地址行5+”;
html+='';
html+='地址行6:'+response.addrLine6+'';
html+='';
html+=“客户邮政编码:”+响应。客户邮政编码+”;
html+='';
$(“#交付”).append(html);
}
});
}
$(文档).ready(函数(){
getthisId(这个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)
未捕获引用错误:未定义此\u id
在HTMLDocument。(1032987988503654:59)
at i(jquery-1.12.3.min.js:2)
在Object.fireWith[as resolveWith](jquery-1.12.3.min.js:2)
在Function.ready(jquery-1.12.3.min.js:2)
在HTMLDocument.K(jquery-1.12.3.min.js:2)
我对此很陌生,任何帮助都会很好:)你应该这样做
'https://thisservice/delivery/?id='+this_id
服务器端需要名为
parcel\u id
的参数中的值,因此需要将该值作为提供给数据的对象中的键,如下所示:
data: { parcel_id: this_id },
为什么他必须这样做?因为它可能以这种方式接受id,而他在以前的方法中已经出现错误。您正在使用查询参数构建url,但如果他使用REST,他的url是有效的url。我接受您的意见,但根据示例,它已经不起作用了,所以上面提到的另一个有效选项。数据:{“parcel_id”:this_id}
如果您的URL实际上是/delivery/12345,那么您需要删除data:parameter,只需使用“../delivery/”+this_id
或可能的”../delivery/“+encodeURIComponent(this_id)
如果it12345中有特殊字符。。。最多16位数字可以是随机的。如果它是一个有效的条形码,这将从json Web服务返回一个结果,硬编码版本使用var this_id=12345,然后url:delivery+this\u id.我现在尝试将此\u id作为url中的参数,而不是从Web服务获取结果的变量,我仍然得到未捕获的引用错误:this\u id未定义@mplungjan然后调用getthisId()或传递null