a7omiton
a7omiton

Reputation: 1617

JQuery getJSON() makes request header url duplicated

I'm getting a duplicated value in my request url when i try and run the below ajax function:

var url = "localhost/travel/home/getPrevApplicantData";
$.getJSON(url, {group_id : "1"})
.done(function(data) {
    console.log( "JSON Data: " + json.address_telephone );
})
.fail(function( jqxhr, textStatus, error ) {
    var err = textStatus + ", " + error;
    console.log( "Request Failed: " + err );
});

The function is inside an onclick event, and I get the following header when the ajax function is run:

Request URL:http://localhost/travel/home/addApplicantVi_pageload/localhost/travel/home/getPrevApplicantData?group_id=1&_=1387835412861
Request Method:GET
Status Code:200 OK

I'm using CodeIgniter as my PHP framework, and the controller method that issues the call to get the data from the models method is in the Home controller and named getPrevApplicantData.

I don't know why the url is being sent back like this, but I'm unable to get any result for my done function using ajax. How do I make it so that I can specify the right url without having it being appended to the current one?

I'm also getting an error in Devtools which points to this line in JQuery:

xhr.send( ( s.hasContent && s.data ) || null );

And my fail function returns:

Request Failed: parsererror, SyntaxError: JSON.parse: unexpected character

But I'm failing to realise where the problem is. If you need more info please let me know, would love to solve this problem.

Upvotes: 0

Views: 380

Answers (2)

Goose
Goose

Reputation: 3279

The url is relative to the current location, if you want an absolute path add the http:// or if you need to go up a level ../getPrevApplicantData.

Upvotes: 1

Alo Sarv
Alo Sarv

Reputation: 376

Your url variable is wrong; it should probably be /travel/home/getPrevApplicantData

URLs starting with / are absolute, starting from domain/server root. URLs without / are considered to be relative to current document, which is why it appends your specified URL to your current document URL.

As far as the additional URL paramater _=1387835412861 is concerned, this is added by jQuery to prevent browsers from caching the response from your queries. This can be controlled by the cache setting in $.ajax() as described at http://api.jquery.com/jQuery.ajax/

Upvotes: 0

Related Questions