backbone.js fetch error - ruby-on-rails

I am facing a problem with getting a response in fetch function of backbone.js. I am using Backbone.js with rails.
$(document).ready(function(){
window.projectPerformanceReport.fetch({
success: function(e) {
$("#loading").hide();
window.questionListView = new QuestionListView;
window.questionListView.render();
window.headerView = new HeaderView;
window.headerView.render();
},
error: function() {
alert("Error");
}
});
});
There is already a URL set for the fetch method and it does make a request to online server which returns a JSON object. I checked by hitting that request independently in new tab and it worked fine. Also the JSON object returned is valid. But when i run this page i always get popup of 'Error'?
Any help would be great.
Thanks

Okay
I got what was the issue..... The thing that was happening was, The Java_Servlet which i was calling to get the JSON object was returning the data in text format instead of JSON... (But it was a valid JSON string)... and while calling the ajax method i had set the type as 'json' so it was failing.
Thanks to everyone who read my question and thought for a solution.
But one strange thing which i observed was, same code was working in production and not in my local machine??? Does anyone have some idea on why it could be behaving like that..???
Thanks :) Now i can sleep properly!!

Related

Pass Response Object on $.mobile.changePage and access it on "pagebeforeshow"

how do we pass an object on $.mobile.changePage and access it on "pagebeforeshow" ???
I am using JQM 1.3.1 and jquery 1.9.1
$.mobile.changePage("Next.html", {data: RespObj});
$(document).on("pagebeforeshow","#next", function(data)
{
alert(JSON.stringify(data));
});
can anyone please help me figure this out. I am not able to access this data inside pagebefore show.
As per I saw, I think you did some syntax mistake with the pagebeforeshow part.
Change your code to the following one and check, hope it will work:
Try this one
$.mobile.changePage("Next.html#next", {data: RespObj});
$(document).on("pagebeforeshow",function(data)
{
alert(JSON.stringify(data));
});
If it works don't forget to VOTE
Thank you and all the best

Restangular PUT not appending the id in the request

I've got an AngularJS App using Restangular for persistence on top of a Rails 4 API. I've run into a problem with my updates not working.
My code in the routeProvider:
when('/course/edit/:courseId', {
controller: 'CourseEdit',
templateUrl: '/assets/templates/course/form.html',
resolve: {
course: function(Restangular, $route) {
return Restangular.one('courses', $route.current.params.courseId).get();
}
}
})
My controller function:
function CourseEdit($scope, $location, course) {
$scope.course = course;
$scope.save = function() {
$scope.course.put().then(function() {
$location.path('/course/view/' + $scope.course.id);
});
};
}
The request for the PUT is being made to http://mysite.com/courses rather than http://mysite.com/courses/id
like I expected it to be, and I can't seem to figure out why.
The error that rails throws out is 'No route matches [PUT] "/courses".
Any help would be greatly appreciated.
I managed to figure it out by poking around at my set up a bit more. I had copied some config for Restangular from a tutorial that I'd found, without really examining whether or not all pieces were necessary.
In my config()
RestangularProvider.setRestangularFields({
id: '_id.$oid'
});
That doesn't match what my fields are in my API, and was the culprit for the problems that I was having. I ended up removing that, leaving only the config for setBaseUrl and it started working again.
Hopefully this will help someone else out who's struggling from the same blindness that I was.

ajax request in rails 2 not working

function ajaxRequest(){
$.ajax({
type: 'GET',
url: 'client/orders/send_mail_to_notaries',
data: { subject: "hi" }
});
return false;
}
It doesn't pass any params in controller.Can any one trigger this out?
Following the question that you have asked today morning page.replace_html method in rails 2 i guess you are using prototype.
Can you check if jQuery is included? Unless jQuery is included this ajax request will not work.
I just tried the method you're using and it worked for me. Perhaps there's a problem with the router/controller?
When debugging ajax it's very handy to use the Chrome developer toolbar. Bring it up, run the javascript and see what happens.
To see the response from the server you can then flip to the Network tab to see what the response is.

server returning 302 moved on create in Rails

I've got a Backbone.js/Rails app and I'm trying to create a new object through Backbone model.save().
The server is returning a 302 moved, but checking the rails controller, the create object exists, and I can navigate to the index, so the routes are properly in place.
I've tried replacing the backbone.js with a regular jquery .ajax function, and both return the 302, as I originally thought the error was in backbone, but I think this is showing that the error is actually in rails.
The request payload/parameters is pretty simple
{"user_id":130,"message_text":"does this go 302","authenticity_token":"GxN8nPf5YwS2j2HhWZxWiKej3Y72Vb5IQZ98u5Nl2gs="}
The backbone save method is
var user_message = new Myapp.Models.UserMessage({
user_id: user.id,
message_text: $('input[name="message"]',this.el).val()
});
user_message.save({
success: function(response) {
new Message({message: response.message});
},
error: function() {
new Error({ message: "adding message" });
}
});
A "302" response has nothing to do with backbone, as you've noted. It looks like you have authorization / authentication code that is causing this.

Post request not working

i've got an android app and a really simple web service that make an insert in a DB with 3 values.
the titanium code is most like the example given on the docs
var xhr = Ti.Network.createHTTPClient();
xhr.onload = function(e) {};
xhr.open('POST','http://www.micheleantonaci.altervista.org/test/foobar.php');
xhr.send({
"latitude":00100,
"longitude":10000,
"type":'From Nexus'
});
and the web service is just
<?php
$con=mysql_connect('http://www.micheleantonaci.altervista.org/','***','***');
mysql_select_db('***',$con);
if(isset($_REQUEST['submit']))
{
$latitude=$_POST['latitude'];
$longitude=$_POST['longitude'];
$kind=$_POST['type'];
$sql="insert into foobar (latitude,longitude,type) values ('$latitude','$longitude','$kind')";
$res=mysql_query($sql) or die (mysql_error());
}
?>
now, when i try the webservice giving the values with the browser it works good, but with the app I get no results at all, any suggestions? tha app doesn't crash or log any error
You must use PHP function json_decode to get values.
Try adding the header like this:
xhr.setRequestHeader("Content-Type", "application/json");
You should also do a var_dump($_POST) in your PHP to see what's in it, not sure you'll get your stuff in separated variables...
When I see a problem like this, I setup 'Charles' or a similar proxy and have the device send it's request through the proxy. Then you can see if the device is send what is expected.
You could also try
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
The code is right, the problem was on the framework itself, at that day the post method wasn't working.
Nowadays it has been fixed.

Resources