0

I'm trying to call a controller function using the .load() function but I'm getting an error probably because of the '.' encoding. I'm doing the call like this:

$("#main-content").load(
   url + "/" + encodeURIComponent(text1) + "/" + encodeURIComponent(text2)
);

Everything goes fine except when text1 ends with a period:

var text1 = 'This causes error.';

The url is encoded like:

http://localhost/index.php/controller/function/This%20causes%20error./Text2

And I'm getting a 404 error in the Chrome console, so I suspect that the '.' is breaking the URL.

I've been looking a lot of posts like this or this, but those solutions doesn't work for me. I'm thinking into create a function that converts the '.' into any other character and then do the opposite in the server, but I prefer to look for something more "elegant" first.

Any ideas? Thanks

Community
  • 1
  • 1
Daniel Marín
  • 1,369
  • 14
  • 36
  • 1
    This is likely not a JS problem, but rather a CodeIgniter issue. See, for example http://stackoverflow.com/questions/20201888/periods-not-allowed-in-codeigniter-uri – Paul Roub Oct 30 '14 at 12:40
  • Yes, I had seen that post, but I tried what they suggested and still have the 404. – Daniel Marín Oct 30 '14 at 14:11

1 Answers1

1

Try using ajax post method

  $.ajax({
        url: url+ + "/",
        type: 'POST',

        data: {data1:encodeURIComponent(text1),data2:encodeURIComponent(text2)},

        success: function (data, status)
        {
               $("#main-content").text(data)

        }
    });
});

Receive text1 and text2 at your controller as

 $text1=$this->input->post("data1")  
 $text1=$this->input->post("data2")  
Shaiful Islam
  • 7,034
  • 12
  • 38
  • 58
  • It worked! I've edited your answer to change the `url + + "/" ` for `url + "/"` and the `.text(data)` for `.html(data)`. Thanks a lot. – Daniel Marín Oct 31 '14 at 09:54