0

When i'm trying to send data(url) to API with ajax request, PHP CI controller not considering all parameters in $_GET. it just accepting up to &.

Is there any error in passing parameters? Please help. I tried passing direct url and encodeURI.

JQuery code

$.ajax({
        type: "GET",
        url: "<?= ROOT?>welcome/ajaxapiget",
        data: encodeURI(http://localhost/webapi/list?categories[]=3&categories[]=12),
        cache: false,
        success: function (data) {
            alert(data);
        }
    });

PHP

function ajaxapiget() {
    $url = $_GET;`//its getting url as http://localhost/webapi/list?categories[]=3`
    $curl = curl_init();
    ........
    ........
    response $response;
}

also tried like this

$.ajax({
        type: "GET",
        url: "<?= ROOT?>welcome/ajaxapiget",
        data: 'url=http://localhost/webapi/list?categories[]=3&categories[]=12)',
        cache: false,
        success: function (data) {
            alert(data);
        }
    });

PHP

function ajaxapiget() {
    $url = $_GET(url);`//its getting url as http://localhost/webapi/list?categories[]=3`
    $curl = curl_init();
    ........
    ........
    response $response;
}

When i alert

before sending request

url=http://localhost/webapi/courselist?categories[]=15&categories[]=17

response from controller (alert at ajax success)

$url = $_GET('url');
echo $url

alert response

http://localhost/webapi/courselist?categories[]=15
Znaneswar
  • 3,329
  • 2
  • 16
  • 24

2 Answers2

0

Try this

JQuery code

$.ajax({
        type: "GET",
        url: "<?= ROOT?>welcome/ajaxapiget",
        data: {url:'http://localhost/webapi/list?categories[]=3&categories[]=12'},
        cache: false,
        success: function (data) {
            alert(data);
        }
    });

PHP code

function ajaxapiget() {
    $url = $this->input->get('url');//Getting $url as http://localhost/webapi/list?categories[]=3&categories[]=12
    $curl = curl_init();
    ........
    ........
    response $response;
}

Use $this->input->get() instead of $_GET[] in Codeingiter

Avi
  • 434
  • 6
  • 20
  • thanks for reply. this is not working. Getting same result(up to &) not the entire url :( – Znaneswar Jul 07 '18 at 05:44
  • Can you show me the response in alert by printing the $_GET do print_r($_GET); die; – Avi Jul 07 '18 at 05:46
  • before ajax request url=http://localhost/webapi/courselist?categories[]=15&categories[]=17 result from controller http://localhost/webapi/courselist?categories[]=15 – Znaneswar Jul 07 '18 at 05:51
  • Are you sure you passed data in ajax request like {url:'http://localhost/webapi/list?categories[]=3&categories[]=12'}and used $this->input->get('url') or $_GET["url"], because there is no method like $_GET(). It should produce error. – Avi Jul 07 '18 at 06:02
  • Yes data i'm sending is same as mentioned but in the controller it not considering from & i tried with normal test string as abcd&efgh but at controller it asscepting only abcd. – Znaneswar Jul 07 '18 at 06:07
  • Thanks for your help finally working with encodeURIComponent – Znaneswar Jul 07 '18 at 06:15
0

Finally i solved this with encodeURIComponent

$.ajax({
    type: "GET",
    url: "<?= ROOT?>welcome/ajaxapiget",
    data: 'url=http://localhost/webapi/list?' + encodeURIComponent('categories[]=15&categories[]=17'),
    cache: false,
    success: function (data) {
        alert(data);
    }
});

now the result is

http://localhost/webapi/courselist?categories[]=15&categories[]=17

encodeURIComponent will encode everything with special meaning, so you use it for components of URIs

Source : https://stackoverflow.com/a/4540785

Znaneswar
  • 3,329
  • 2
  • 16
  • 24