开发者

jQuery ajax posting to wrong url in rails app

$.ajax({
 type: "POST",
 url: "analytics/test",
 data: 'correct=' + correct_or_incorrect + '&id=' + correctCharacter[3] + "'"
 });

I'm using this to post some information to a rails app. Problem is that it is just posting to the application controller.

How can I get it so that the url开发者_如何学Go is positing to :controller => 'analytics', :action => 'test'


The path to your url (analytics/test) is relative, meaning it will be appended to the "directory" of the url you are working in. E.g. if you are calling this from http://yourhost.com/analytics/demo, you will call http://yourhost.com/analytics/analytics/test, which you probably do not want.

Add a slash to the beginning (so you get /analytics/test) and you're fine.

$.ajax({
    type: "POST",
    url: "/analytics/test",
    data: 'correct=' + correct_or_incorrect + '&id=' + correctCharacter[3] + "'"
});


None of the answerees know about rails and routes... at least that is what I'm assuming because nobody got close to this one.

$.ajax({ 
    url: '/analytics.json', 
    type: 'POST', 
    dataType: 'json', 
    success: 'success' 
 });

That's the answer :) - makes me very happy! Two days to get that one.

The problem was because of the url. I had :analytics declared in routes.rb but I didn't have the url correct and that is how rails deals with routes in particular RESTful routing.

Anyone using the code above should not that they can use html xml or whatever but you need to use that as the file extension instead of using .json

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜