S
S
Serginyo902016-09-08 15:51:23
JavaScript
Serginyo90, 2016-09-08 15:51:23

From curl request to JSON?

Hello. How to send request from CURL to JSON format?

curl --header "Authorization: key=AIzaSyA5lt6V7fkbX0uNdj3b6mM4PFfZ7LDEFq8" --header "Content-Type: application/json" https://android.googleapis.com/gcm/send -d "{\"registration_ids\":[\" eplUw7pEIek :APA91bFWVuxnOyqViPnBqoUQjOf7Sh-ea4nehC... "]}"

Answer the question

In order to leave comments, you need to log in

5 answer(s)
G
gleendo, 2016-07-24
@DimaPolishuk

Working example - https://jsfiddle.net/evg_/sn1akyhz/1/

var btn = document.querySelector(".btn")
    isStart = false;

btn.addEventListener("click", function() {
  if (isStart) {
    btn.classList.remove("modifier");
    btn.textContent = "Старт";
    isStart = false;
    stop();
    return;
  };
  
  btn.classList.add("modifier");
  btn.textContent = "Стоп";
  isStart = true;
  start();
});

function start() {
  console.log("start");
}

function stop() {
  console.log("stop");
}

A
Andrey Fedorov, 2016-07-24
@aliencash

Do you really need js?
codepen.io/aliencash/pen/ZOoBQX

I
iNeedHelp, 2016-07-24
@iNeedHelp

<button id='start' value='Start'></button>

let elem = document.querySelector('#start');
elem.addEventListener('click', () => {
 elem.value = 'Stop';
 elem.backgroundColor = 'red';
});

V
Vladislav Polyakov, 2016-07-24
@polRk

Example
Include jquery

var btn = $('#btn');
btn.on('click',function(){
  btn.hasClass('green') ? red(btn) : green(btn);
});
function green(el){
  el.removeClass();
  el.addClass('green');	
}
function red(el){
  el.removeClass();
  el.addClass('red');
}
$('.green').on('click', function(){
//Some Function
});
$('.red').on('click', function(){
//Some Function
});

T
theg4sh, 2016-09-15
@theg4sh

On the subject of the question: it is logical to assume that if the response does not fall into the done callback, then there are two branches of events, either problems in jQuery, which is unlikely, or an error code is returned from the server.
In both cases, the response will be given by the Net tab in FireBug, which will show the request, the request headers sent, and the response.
Now to the nuances: there is such a thing as a cross-domain request , in which the Access-Control-Allow-Origin header in the server response (which is described in the link) plays a key role. If it is not set to "*" or your domain is not specified or there is no Origin header, then no matter how much you want to receive an answer, it will not be.
Serginyo90 correct the block headers: { "Authorization": "key=...." } in Dmitry 's comment .
there is an extra colon, then most likely you will pass authorization and the Unauthorized response will not be returned and everything will work.
You should also pay attention to the $.ajax parameters: dataType, success, error, complete and crossDomain to start with.
In the example from Dmitry , with the error callback parameter set, the response will fall there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question