我得到了一个类似这样的URL
http://m.example.com/user/login我的老师告诉我,我必须调用这个URL,并使用ajax或Jquery从完全不同的网站使用用户名和密码登录。
这是一个做很多事情的web服务,现在我只需要登录它。
当我调用一个url时,它会出现这样的错误:
http://m.example.com/user/login?u=blah&p=blah有人知道我该怎么做吗?
我需要做一些php吗?
或者只用ajax和html就可以做到?
我已经这样做了:
$(document).ready(function(){
$("#id").click(function(e){
e.preventDefault();
$.ajax({
type: "POST",
url: "http://m.example.com/user/login",
data: "name="+ $('input[name='u']').val() +"&pwd="+ $('input[name='p']').val() , // change the param names according to your teacher
success: function(response){
// do what ever you want with the response
},
});和html:
<form method="post">
First name:<br>
<input type="text" name="u" value="blah">
<br>
Last name:<br>
<input type="password" name="p" value="blah">
<br><br>
<input type="button" id="id" value="Submit">以及错误:
{"error": "Shell form does not validate{'html_initial_name': u'initial-js_lib', 'form': <mooshell.forms.ShellForm object at 0x207bfd0>, 'html_name': 'js_lib', 'html_initial_id': u'initial-id_js_lib', 'label': u'Js lib', 'field': <django.forms.models.ModelChoiceField object at 0x2238110>, 'help_text': '', 'name': 'js_lib'}{'html_initial_name': u'initial-js_wrap', 'form': <mooshell.forms.ShellForm object at 0x207bfd0>, 'html_name': 'js_wrap', 'html_initial_id': u'initial-id_js_wrap', 'label': u'Js wrap', 'field': <django.forms.fields.TypedChoiceField object at 0x2238190>, 'help_text': '', 'name': 'js_wrap'}"}发布于 2015-11-24 14:12:11
既然你听起来像个初学者,你应该问老师几个问题。
首先,请求将参数名传递给登录服务需要的服务器。
然后使用以下ajax将值传递给URL,以便调用登录服务。
<form method="post">
First name:<br>
<input type="text" name="u" value="blah">
<br>
Last name:<br>
<input type="password" name="p" value="blah">
<br><br>
<input type="button" value="Submit">
</form>
$.ajax({
type: "POST",
url: "http://m.example.com/user/login",
data: "name="+ $('input[name='u']').val() +"&pwd="+ $('input[name='p']').val() , // change the param names according to your teacher
success: function(response){
// do what ever you want with the response
},
});表单不应该包含提交,而应该包含一个按钮。使用ajax向该按钮添加一个单击事件。这应该会使脚本运行。
响应要么是成功的JSON,要么是失败的响应,或者是页面重定向。只需执行一个console.log(response)来查看从服务器返回的内容。
https://stackoverflow.com/questions/33886509
复制相似问题