Javascript 像邮递员一样发送jquery请求
我试图从javascript文件中发送一些Javascript 像邮递员一样发送jquery请求,javascript,java,jquery,http,postman,Javascript,Java,Jquery,Http,Postman,我试图从javascript文件中发送一些post方法,但出现了一些问题,脚本无法工作。我的html文件是: <html> <head> <script src="https://code.jquery.com/jquery-1.9.1.min.js"></script> <script> $( document ).ready(function() { var request = $.ajax(
post
方法,但出现了一些问题,脚本无法工作。我的html
文件是:
<html>
<head>
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script> <script>
$( document ).ready(function() {
var request = $.ajax({
method: "POST",
url: "http://localhost:8081/login",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: { username: "John", password: "Boston" }
})
.done(function( msg ) {
alert( "Done: " + msg );
})
.fail(function( jqXHR, textStatus ) {
alert( "Request failed: " + textStatus );
});
});
</script>
</head>
<body>
</body>
</html>
如果我从html
文件body
和contentType
发送请求,则请求为空,headerpart
为text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
但是,当请求方法从Postman发送时,一切都很正常(因此contentType
是text/plain;charset=UTF-8
和body
是{“用户名”:“admin”,“密码”:“admin”}
),以下是我的Postman配置:
我可能误解了某些内容,但不知道为什么jquery的请求不一样。有什么解决方案吗?成功的邮递员请求表明您的后端设置正确。不过,您的请求方法可能有问题。删除第5行中使用的变量绑定 这里有一个类似问题的链接。 指向w3学校用例的链接 您的代码应该如下所示:
$( document ).ready(function() {
$.ajax({
method: "POST",
url: "http://localhost:8081/login",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: { username: "John", password: "Boston" }
})
.done(function( msg ) {
alert( "Done: " + msg );
})
.fail(function( jqXHR, textStatus ) {
alert( "Request failed: " + textStatus );
});
});
成功的邮递员请求表明您的后端已正确设置。不过,您的请求方法可能有问题。删除第5行中使用的变量绑定 这里有一个类似问题的链接。 指向w3学校用例的链接 您的代码应该如下所示:
$( document ).ready(function() {
$.ajax({
method: "POST",
url: "http://localhost:8081/login",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: { username: "John", password: "Boston" }
})
.done(function( msg ) {
alert( "Done: " + msg );
})
.fail(function( jqXHR, textStatus ) {
alert( "Request failed: " + textStatus );
});
});
您可以尝试使用ajax请求的数据类型作为数据类型:“text”而不是数据类型:“json”。您希望从服务器返回的数据类型。我尝试了,但没有帮助。您可以尝试使用ajax请求的数据类型作为数据类型:“text”而不是数据类型:“json”。您希望从服务器返回的数据类型。我尝试了,但没有帮助