Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No data posted when type is 'json' #10

Closed
Drumkruk opened this issue Nov 13, 2014 · 4 comments
Closed

No data posted when type is 'json' #10

Drumkruk opened this issue Nov 13, 2014 · 4 comments

Comments

@Drumkruk
Copy link

When posting data through ajax with the following setup:

ajax(
{
  url: URL,
  method: 'post',
  type: 'json',
  data: {
    auth : 'test'
  }
},
function(data) {
  // Success!
  console.log(JSON.stringify(data));
},
function(error) {
  // Failure!
  console.log('no response');
}
);

Php is not receiving any post data. Shouldn't the Content-Type header be application/x-www-form-urlencoded as it is sending data? Now it is specifically set to 'application/json' in the code, which doesn't post the data?

The post data is send when the type is 'html'.

Please help me out, because I really dig Pebble.js

@sarfata
Copy link

sarfata commented Nov 14, 2014

It will post the data as json but not as urlencoded form data. This is a common way to do this for json webservice but maybe not what you were expecting.

See: https://github.com/pebble/pebblejs/blob/develop/src/js/lib/ajax.js#L84-L86

@Meiguro
Copy link

Meiguro commented Nov 14, 2014

Thanks for responding Thomas.

That's right, the 'json' type is when both client and server use json. Similarly, 'form' is when both use form. Thus type is a convenience parameter for when only a single format is used throughout. In the case of sending form data and receiving json data, leaving the type undefined or specifying 'text' and using JSON.parse for received data is best. It can be annoying to have to repeat this pattern so wrapping this in a helper function might help.

I'll be sure to make this more clear in the documentation.

@Drumkruk
Copy link
Author

Interesting. It would be good to put it in the documentation that the output is also changed by the 'type'. I am now storing the data from the ajax request in php with the following code:

json_decode(file_get_contents('php://input'), true);

@Meiguro
Copy link

Meiguro commented Feb 3, 2015

Thanks, the documentation has been updated! It sort of shoves a lot of into into that little box -- I should structure it better in the future.

@Meiguro Meiguro closed this as completed Feb 3, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants