我有4个文件:
HTML表单
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="<?php base_url(); ?>ajax/request.js"></script>
<title>Welcome to CodeIgniter</title>
</head>
<body>
<h1>CRAWLER</h1>
<textarea cols="60" rows="5" id="wartosc"></textarea>
<br>
<button>Send</button>
</body>
</html>
JS档案
$(document).ready(function () {
$('button').click(function () {
var zmienna = $('#wartosc').val();
$.ajax({
url: window.location+"/curl/",
type: "POST",
data: {
wartosc: zmienna
},
success: function (odp) {
$('#wynik').html(odp);
}
})
})
});
Codeigniter卷曲控制器
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Curl extends CI_Controller
{
public function index()
{
if (isset($_POST['wartosc'])) {
$adresy = explode("\n", $_POST['wartosc']);
foreach ($adresy as $lista_adresow) {
$this->get_data($lista_adresow);
}
}
}
function get_data($adres)
{
$url = $adres;
$c = curl_init();
$opcje = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => TRUE
);
curl_setopt_array($c, $opcje);
curl_exec($c);
$info = curl_getinfo($c);
$tablica = array() + $info;
if ($info['redirect_url'] != NULL) {
$this->get_data($info['redirect_url']);
}
return $tablica;
}
}
和Codeigniter测试控制器
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller
{
public function index()
{
$data['val_from_ajax']=//need value from curl controller here
$this->load->view('view',$data);
}
}
我使用ajax从表单向curl控制器发送值,并将结果($ tablica)返回到HTML表单,但我需要在Test控制器中使用此数组。
问题: 我怎么能改变jquery.ajax()函数来返回请求不要形成文件,只测试控制器? 我很难解释我需要做什么,对不起这个
谢谢
答案 0 :(得分:0)
如果我错了,请先纠正我。如果您已经创建了Curl控制器来执行curl命令,那么您也可以在Test controller中执行这些操作。像
这样的东西
class Test extends CI_Controller
{
public function index()
{
if (isset($_POST['wartosc']))
{
$adresy = explode("\n", $_POST['wartosc']);
$needed_value = $this->get_data($adresy);
}
$data['val_from_ajax']= $needed_value;//need value from curl controller here
$this->load->view('view',$data);
}
function get_data($adres)
{
foreach ($adres as $lista_adresow)
{
$url = $lista_adresow;
$c = curl_init();
$opcje = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => TRUE
);
curl_setopt_array($c, $opcje);
curl_exec($c);
$info = curl_getinfo($c);
$tablica = array() + $info;
if ($info['redirect_url'] != NULL) {
$this->get_data($info['redirect_url']);
}
}
return $tablica;
}
}
上面的代码粗略完成。您可能需要更正适当更改ajax调用。如果有效,请告诉我们