1

i tried to send data from view to controller , but nothing work "ajax return error" i try all ways those i see in other similar question My view:

var pdata ={
    "latitude": lat,
    "longitude": log
};
latlog = "/"+1;
$.ajax({
    url: "<?php echo base_url(); ?>index.php/users/search"+CityINP+distINPx+CategoryINP+textINP+latlog,
    type: "POST",
    data: pdata,
    dataType: "JSON",
    success: function (data) {
        // if success reload ajax table
         alert("success");
        // reload_table();
    },
    error: function (jqXHR, textStatus, errorThrown) {
        alert('Error adding / update data');
    }
});

my controller:

public function search($city=null,$dist=null,$cate=null,$text=null,$latlon=null)
{
}
4
  • You can check the contents of textStatus and errorThrown. Ajax may tell you itself what's wrong, this is what those parameters are for. Commented Mar 23, 2018 at 22:02
  • 2
    what is the actual issue that you are facing?? Commented Mar 23, 2018 at 22:25
  • I think it might be with your ajax's url, check your slashes / between the parameters Commented Mar 24, 2018 at 10:50
  • @MAZux Parameters already had slash Commented Mar 24, 2018 at 15:39

2 Answers 2

2

At first, I will suggest you to make a js variable in header section like this

<script type="text/javascript">
        var BASE_URL = "<?php echo base_url(); ?>";
</script>

In this way, you won't have to write echo baseurl everytime in the URL.

$.ajax({
    type: 'POST',
    url: BASE_URL + "controllerName/search",
    data: {'CityINP': distINPx,'CategoryINP':latlog },
    success: function (data) {
    data = JSON.parse(data);
    if(data.response == "response")
    {
        //do anything you want from the response you get
    }
 }
});

To get the parameters in the controller function you can get it by the following code

$this->input->post('CityINP');
$this->input->post('CategoryINP');
Sign up to request clarification or add additional context in comments.

Comments

0

change your url url: "index.php/users/search"+CityINP+distINPx+CategoryINP+textINP+latlog,

to url: "index.php/users/search/"+CityINP+distINPx+CategoryINP+textINP+latlog,

"/" after search function name, in your url all variables are concat with search funaction name.

1 Comment

why?? if url doesn't get its correct function name how data will pass ??? and value will return?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.