[英]Javascript AJAX call to Jquery Call
In order to have my code written almost fully written in Jquery, I want to rewrite an AJAX call in Jquery. 为了让我的代码几乎完全用Jquery编写,我想在Jquery中重写一个AJAX调用。
It's a call from a webpage to a Tomcat servlet. 这是从网页到Tomcat servlet的调用。
Similar code of my present situation: 我目前情况的类似代码:
var http = new XMLhttpRequest();
var url = "http://example.com/MessageController?action=send";
http.onreadystatechange = function ()
if (http.readyState == 4)
{
if (http.status == 200){ var response = http.responseText;}
else {/*code2*/}
};
http.async = false;
http.open("POST", url, true);
http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
http.send(string);
Which would be the best way to do this? 这是最好的方法吗?
.ajax
or .post
? .ajax
或.post
? Could you help me with some pseudo code to start this? 你能帮我一些伪代码来启动它吗?
Use .ajax
or (as you are using POST) .post
: 使用
.ajax
或(当你使用POST时) .post
:
$.ajax({
url: "http://example.com/MessageController",
type: "POST",
data: { action: "send", yourStringName: "something" },
async: false, // make sure you really need it
contentType:'application/x-www-form-urlencoded'
}).done(function(response) {
console.log(response);
});
It doesn't matter which one you use, because .post
is shorthand for .ajax
. 你使用哪一个并不重要,因为
.post
是.ajax
简写。
You can use jQuery post . 你可以使用jQuery 帖子 。
var url="MessageController"
$.post(url, { action : "send"} ,function(data){
//response from MessageController is in data variable
//code 1
alert(data)
}).error(function(){
alert("error"); //code 2
})
Assuming MessageController
is some url in your domain and you are aware of the same origin policy 假设
MessageController
是您域中的某个URL,并且您知道相同的源策略
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.