Jump to content

backbone fetch command testing


jimfog

Recommended Posts

I am trying to send to the server something using backbone's fetch command....

This is the code:

events.fetch({ data: { service: 'service'}});

It is a JSON string as you can see.

In the PHP script where the above goes I have this test code to see that the above actually goes to the server:

$test=$_GET['service'];var_dump($test); 

Unfortunately the server responds with the usual error undefined index: service.

And this happens(probably) because this is what is appended to the end of the URL which I cannot understand why:

[object%20Object]

 

Normally in the end of the URL there should be the string service...instead I see the above.

The above would make sense(I think) if there was something wrong with the syntax....but I think the syntax is OK.

 

What do you think?

Link to comment
Share on other sites

what you are sending is not a JSON string. What you are sending is a JSON object, which is exactly what you are seeing in the URL. If you are sending JSON like that, you might need to stringify it first.

JSON.stringify({ data: { service : 'service' } });

But usually you send JSON as part of the request payload / body, like for a POST or PUT request. I am not familiar with fetch, though it seems like it would be a GET

Link to comment
Share on other sites

let's start clarifying some things and ask some questions.

 

  1. fetch() involves a GET request
  2. I do not use REST...despite I use backbone.
  3. Regarding the syntax you see(without JSON.stringify), I asked the backbone google group and someone proposed this.

Yes, you are right,JSON is usually sent in the payload with POST/PUT.

As I said above though fetch() is a GET request and I do not think I can change that.

 

And yes you are right...a JSON object is sent.I can see that in the dev tools now.

Still cannot understand the diff between a JSON string and an object.

Edited by jimfog
Link to comment
Share on other sites

Still cannot understand the diff between a JSON string and an object.

Regardless of JSON, one is a string of text and the other is an object. On the internet data is sent in text, you don't send literal objects and arrays around the place. You send text, and using JSON is a way to send a piece of text that can be converted into an object or array on the other end.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...