---
title: "mediawiki 实现ajax请求及demo"
author: "qepwqnp"
date: "2015-04-01T14:36:49Z"
tags: []
categories: ["编程语言"]
slug: "mediawiki-ajax-demo"
draft: false
lastmod: "2015-04-01T14:38:02Z"
id: 2197686
url: "https://chengjunflying.iteye.com/blog/2197686"
---

实现方案为使用mediawiki 的api。

# 1．前台发起ajax请求

```
$req = {
format: 'json', action: 'req',
    titles: 'testPage',fun:'test',
};
$.ajax({
    url: '/api.php',
    data: $req, cache: true, dataType: 'jsonp',
    success: function(result) { 
    	/* handle errors and warnings, process content. */ 
    	alert(result["result"]["pay"]["isPay"]);
//打印结果为1
    	}
});
```

 

 

# 2．后台开发ApiCommon.php

```
<?php
/**
 *
 * @ingroup API
 */
class ApiCommon extends ApiBase {
 
public function execute() {
$request = $this->getRequest();
$fun = $request->getval( 'fun' );
//例子
if($fun == "test"){
$this->test();
}
}
public function test(){
$request = $this->getRequest();
$titles = $request->getval( 'titles' );
$result = $this->getResult();
$result->addValue( array(
'result',
'pay'
), "isPay", "1" );
}
}
```

 

 

# 3．Action注册ApiMain.php

```
'req' => 'ApiCommon',
```

 

 4．后台类注册AutoLoader.php

```
'ApiCommon' => 'includes/api/ApiCommon.php',
```

 

 

 

# 5．抓包信息

 

Request URL:

 

```
http://localhost:7070/api.php?callback=jQuery1111030762016819790006_1422411110274&format=json&action=req&titles=testPage&fun=test
```

 

 

Response

 

```
/**/jQuery1111030762016819790006_1422411110274({"warnings":{"main":{"*":"Unrecognized parameters: 'titles', 'fun'"}},"result":{"pay":{"isPay":"1"}}})
```