请看图片http://whatcodecraves.com/images/backbonejs-auth-http.png,
我有 2 个 AJAX 请求:
第一个进行身份验证,服务器响应:
Set-Cookie:JSESSIONID=06AB56403C08AD373BEDE33CBEFBE102; Path=/
另一个使用会话。我应该怎么做才能发送到服务器以使用相同的身份验证会话(GET 请求)?
我的代码:
第一个 AJAX 请求:
$.ajax({ url : "https://www.hafiz.gov.sa/HRDFWeb/AuthenticateDirectly", type : "POST", data : { "date" : "135854809", "json" : JSON.stringify({ "nin" : "1089194383", "password" : "255793" }) }, success : function(data) { console.log(data); } });
第二个 AJAX 请求:
$.ajax({ url : 'https://www.hafiz.gov.sa/HRDFWeb/UpdateLastLogin?nin=1089194383&password=255793&date=1374661464&callback=?', type : "get", dataType: 'jsonp', success : function(data) { console.log(data); }, error : function(data){ console.log(data); } });
编辑:
@w4rumy,感谢您的回答,但我无法访问该代码。我正在编写一个 phonegap android 应用程序,这是它用来连接到服务器的代码。
HttpPost httpost = new HttpPost("https://www.hafiz.gov.sa/HRDFWeb/AuthenticateDirectly");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("date", System.currentTimeMillis() + ""));
nvps.add(new BasicNameValuePair("json", "{\"nin\":\""+idd+"\",\"password\":\""+passs+"\"}"));
try {
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
response = httpclient.execute(httpost);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
entity = response.getEntity();
Log.v("mytag", "Login form get: " + response.getStatusLine());
if (entity != null) {
try {
entity.consumeContent();
} catch (IOException e) {
e.printStackTrace();
}
}
Log.v("mytag", "Post logon cookies:");
cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
Log.v("mytag", "None");
} else {
for (int i = 0; i < cookies.size(); i++) {
Log.v("mytag", "- " + cookies.get(i).toString());
}
}
httpget = new HttpGet("https://www.hafiz.gov.sa/HRDFWeb/UpdateLastLogin?nin=1089194383&password=255793&date=1358448809");
try {
response = httpclient.execute(httpget);
} catch (ClientProtocolException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
content = response.getEntity().getContent();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}