[英]How to send a GET request to webservice from javascript
I have an html form of which has an onClick attribute on it's button, that then calls a javascript function: 我有一个HTML表单,该表单的按钮上具有onClick属性,然后调用了javascript函数:
function submitForm () {
// Get Session ID
var sessionId = document.getElementById("session-id").value;
// Get Description
var description = document.getElementById("description").value;
// Call WebService ??
}
At this stage I have to call a webservice at this URL: 在此阶段,我必须通过以下URL调用网络服务:
localhost:8080/PatientRefund/WebService?urnId=93&batchDescription=My%20Description
Where you can replace '93' with 'sessionId' and 'My%20Description' with 'batchDescription'. 您可以在其中将“ 93”替换为“ sessionId”,将“ My%20Description”替换为“ batchDescription”。 Also the request should be a GET request.
该请求也应该是GET请求。
Can anyone just point me in the right direction? 谁能指出我正确的方向? I'm not looking for you to write the code for me... Thanks guys :)
我不是要你为我写代码...谢谢大家:)
Using jQuery ajax, your code will be like: 使用jQuery ajax,您的代码将类似于:
function submitForm () {
var sessionId = document.getElementById("session-id").value;
var description = document.getElementById("description").value;
$.ajax({
url: 'http://localhost:8080/PatientRefund/WebService',
type: 'GET',
data: {urnId: sessionId, batchDescription: description}
}).done(function(response) {
console.log(response);
// do something with your response
});
}
If you want the clients to download a file after submit, you should not use ajax request. 如果希望客户端在提交后下载文件,则不应使用ajax请求。 You should create a form dynamically and submit it.
您应该动态创建一个表单并提交。 Example code:
示例代码:
function submitForm() {
var sessionId = document.getElementById("session-id").value;
var description = document.getElementById("description").value;
var newForm = jQuery('<form>', {
'action': 'http://localhost:8080/PatientRefund/WebService',
'method': 'GET',
'target': '_top'
}).append(jQuery('<input>', {
'name': 'urnId',
'value': sessionId,
'type': 'hidden'
})).append(jQuery('<input>', {
'name': 'batchDescription',
'value': description,
'type': 'hidden'
}));
newForm.submit();
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.