Javascript href中调用ajax请求函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29824467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling Ajax request function in href
提问by Lotus91
I have an href in an html page and i have an AJAX request in a method in a javascript file.
我在一个 html 页面中有一个 href 并且我在一个 javascript 文件中的一个方法中有一个 AJAX 请求。
When clicking on href i want to call the JS function and I am treating the response to add it to the second html page which will appear
单击 href 时,我想调用 JS 函数,我正在处理响应以将其添加到将出现的第二个 html 页面
function miniReport(){
alert('TEST');
var client_account_number = localStorage.getItem("numb");
var request = $.ajax({
url: server_url + '/ws_report',
timeout:30000,
type: "POST",
data: {client_language: client_language, PIN_code:pin,client_phone:number}
});
request.done(function(msg) {
//alert(JSON.stringify(msg));
});
if (msg.ws_resultat.result_ok==true)
{
alert('success!');
window.open("account_details.html");
}
request.error(function(jqXHR, textStatus)
{
//MESSAGE
});
}
I tried with <a href="#" onclick="miniReport()"></a>, and also to write the function with $('#idOfHref').click(function(){});not working.
我尝试使用<a href="#" onclick="miniReport()"></a>, 并且还编写了不起作用的函数$('#idOfHref').click(function(){});。
All I can see is the alert TEST and then nothing happens. I checked several posts here but nothing works for me.
我只能看到警报 TEST,然后什么也没有发生。我在这里查看了几篇文章,但没有任何内容对我有用。
回答by Vikrant
Function can be corrected as,
函数可以修正为,
function miniReport(){
alert('TEST');
var client_account_number = localStorage.getItem("numb");
$.ajax({
url: server_url + '/ws_report',
timeout:30000,
type: "POST",
data: {"client_language": client_language, "PIN_code":pin,"client_phone":number},
success : function(msg) {
//alert(JSON.stringify(msg));
if (msg.ws_resultat.result_ok == true)
{
alert('success!');
window.open("account_details.html");
}
},
error: function(jqXHR, textStatus)
{
alert('Error Occured'); //MESSAGE
}
}
});
1. No need to assign ajax call to a variable,
2. Your further work should be in Success part of AJAXrequest, as shown above.
1.无需将ajax调用分配给变量,
2.您的进一步工作应该在AJAX请求的成功部分,如上所示。
回答by gon250
It's a bad practice use an onclick()so the proper way to do this is:
使用 an 是一种不好的做法,onclick()因此执行此操作的正确方法是:
$(document).ready(function(){
$('#mylink').on('click', function(){
alert('onclick is working.');
miniReport(); //Your function
});
});
function miniReport(){
var client_account_number = localStorage.getItem('numb');
$.ajax({
url: server_url + '/ws_report',
timeout:30000,
type: "POST",
data: {
'client_language': client_language,
'PIN_code': pin,
'client_phone': number
},
success: function(msg){
if (msg.ws_resultat.result_ok==true)
{
alert('success!');
window.open("account_details.html");
}
},
error: function(jqXHR, textStatus)
{
//Manage your error.
}
});
}
Also you have some mistakes in your ajax request. So I hope it's helps.
您的 ajax 请求中也有一些错误。所以我希望它有帮助。
回答by Neelesh
Rectified version of your code with document .ready
使用文档 .ready 修正您的代码版本
$(document).ready(function(){
$("#hrefid").click(function(){ // your anchor tag id if not assign any id
var client_account_number = localStorage.getItem("numb");
$.ajax({
url: server_url + '/ws_report',
timeout:30000,
type: "POST",
data:{"client_language":client_language,"PIN_code":pin,"client_phone":number},
success : function(msg) {
if (msg.ws_resultat.result_ok == true)
{
window.open("account_details.html");
}
else
{
alert('some thing went wrong, plz try again');
}
}
}
});
});

