0

I'm actually trying to do a dynamic administration about teams in my project. It means for example, the user has 3 buttons. Each buttons calls a function from a controller that displays a form.

However, when the form appears, the url is wierd when i press submit.

function ajax_get(){

var uri_segment = "<?=$this->uri->segment(3);?>";

$('#editTeam').click(function (e) {
    e.preventDefault();

    $.ajax({
        type : "GET",
        async : false,
        url : "<?=base_url().'ajax/team/get_editTeam/';?>"+uri_segment,
        dataType : "html",
        success : function(data){
            window.setTimeout(function () {
                $('#divPage').html(data);
            }, 300);

        }
    });
    return false;
});

This function display a view in a div, and then i have a form

function ajax_editTeam()
{
$('form').submit(function(e){
    e.preventDefault();

    $.ajax({
        type : "POST",
        async : false,
        url : "www.google.fr",
        data : $(this).serializeArray(),
        dataType : 'json',
        success : function(data)
        {

        }
    });


    return false;
});
 }

For this form I have another ajax call that save the field and update the team informations. As you can see I wrote 'www.google.fr' as url but it changes nothing

When I submit the form, I have a wierd url in firebug.

This is the console from firebug : GET http://fresh-league.com/ajax/team/get_editTeam/30 200 OK 164ms
POST http://fresh-league.com/ajax/team/editTeamget_editTeam 404 Not Found 354ms

Can you please tell me why I have another requested url than www.google.fr ?

Thanks for answers Matt

2 Answers 2

1

It is because of Same Origin Policy restrictions on the browser.

Same Origin Policy restrict browsers from making ajax request to a domain other than form which the source script was downloaded.

There are multiple ways to overcome this

  1. Use jsonp
    If the requesting resource supports jsonp then you can use it, jquery has built in support for jsonp. for using jsonp you need to pass a request parameter callback=? and set the datatype as jsonp or use getJSON to request a json resource.

  2. Use CORS

Sign up to request clarification or add additional context in comments.

Comments

0

You have the answer in your question,

this is correct: url : "<?=base_url().'ajax/team/get_editTeam/';?>"+uri_segment

and this isn't: url : "www.google.fr" because you are not calling any function from your controller, try giving it a url like the first ajax call.

1 Comment

Hi The www.google.fr was an example, I think i would have another type of error in my console, like 401 unauthorized. I don't understand why the url parameter in the second request isn't 'read' by jquery I always have this url : fresh-league.com/ajax/team/editTeamget_editTeam instead of fresh-league.com/ajax/team/edit/30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.