我用java方法编写了负责发送的POST
方法ajax
public String getWebInfo() {
DefaultHttpClient httpclient = null;
String out = null;
try {
System.out.println("send started");
httpclient = new DefaultHttpClient();
Credentials cred = new UsernamePasswordCredentials("user", "password");
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
cred);
HttpPost httpost = new HttpPost("https://page/php/data.ajax.php");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("action", "1"));
nvps.add(new BasicNameValuePair("name", "2"));
UrlEncodedFormEntity entityU = new UrlEncodedFormEntity(nvps);
entityU.setContentEncoding(HTTP.UTF_8);
//entityU.setContentType("application/json");
httpost.setEntity(entityU);
System.out.println("send about to do post");
HttpResponse response = httpclient.execute(httpost);
System.out.println("send post done");
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println("response.getStatusLine: " + response.getStatusLine());
InputStream is = entity.getContent();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ( (line = br.readLine()) != null) {
System.out.println("Response: " + line);
out = line;
}
is.close();
} else {
System.out.println("Response: response is null");
}
} catch (Exception e) {
e.getStackTrace();
}
if (httpclient != null) {
httpclient.getConnectionManager().shutdown();
}
return out;
}
输出为空:
send started
send about to do post
send post done
response.getStatusLine: HTTP/1.1 200 OK
Response: []
服务器端
从我的登录msqLogFile("page/post",Array('post' => urldecode(implode(", ", $_POST))));
我只得到没有密钥的数据:
"2013-05-02 04:21:09","1,2"
它应该是 json 数据:
从 tail -f /etc/httpd/logs/ssl_request_log
82.80.25.130 TLSv1 AES128-SHA "POST /page/php/data.ajax.php HTTP/1.1" 484
82.80.25.130 TLSv1 AES128-SHA "POST /page/php/data.ajax.php HTTP/1.1" 3
当我取消注释时entityU.setContentType("application/json");
,我根本没有得到任何数据。
从javascript一切正常:
var json = JSON.stringify({action: 1,name: 2});
$.ajax({
type: "POST",
url: "/page/php/data.ajax.php",
dataType:"json",
data:{data:json},
success:function(data){
在这里我得到回应:
"2013-05-02 04:21:09","{"action":1,"name":2}"
我的问题在哪里?
谢谢,