Title: Post request with ajax for Firebase Cloud Messaging
Tags: javascript;html;ajax;firebase-cloud-messaging
Question: I have writtent this script to send notification from my HTML page.
When i try to send the request, i get both messages ```"Success"``` and ```"Fail"``` and the notification is not sent. I call the function ```get_data_for_notification()``` on button click.

```<script src="http://code.jquery.com/jquery-1.11.0.min.js"&gt;</script&gt;
  <script type="text/javascript"&gt;
    function get_data_for_notification(){
        var title = document.getElementById('news_title').value;
        var subtitle = document.getElementById('news_small_description').value;

        $.ajax({
                 type : 'POST',
                 url : "https://fcm.googleapis.com/fcm/send",
                 headers : {
                     Authorization : 'key=mykey'
                 },
                 contentType : 'application/json',
                 dataType: 'json',
                 data: JSON.stringify({"to": "/topics/android",  "priority" : "high", "notification": {"title":title,"body":subtitle}}),
                 success : alert("Success")            ,
                 error : alert("Fail")
             }) ;
    }
  </script&gt;
```


Here is the accepted answer: The problem was on the button type, i needed to use ```type="button"``` and not ```type="submit"```.


Here is another answer: Your script executes, before sending the request, ```alert("Success")``` and ```alert("Fail")``` and assigns their return value as the event handlers.  

You have to wrap them in an anonymous function:

```    $.ajax({
        type: 'POST',
        url: "https://fcm.googleapis.com/fcm/send",
        headers: {
            Authorization: 'key=mykey'
        },
        contentType: 'application/json',
        dataType: 'json',
        data: JSON.stringify({
            "to": "/topics/android",
            "priority": "high",
            "notification": {
                "title": title,
                "body": subtitle
            }
        }),
        success: function(responseData) {
            alert("Success");
        }
        error: function(jqXhr, textStatus, errorThrown) {
            /*alert("Fail");*/   // alerting "Fail" isn't useful in case of an error...
            alert("Status: " + textStatus + "\nError: " + errorThrown);
        }
    });
```

Comment for this answer: Check the request and response in the network tab of your developer tools, check the documentation on how to make a valid request, ...
Comment for this answer: Sorry it was a typo... but now i only get an alert `Status: Error, Error`.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:08/04/18', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<javascript><html><ajax><firebase-cloud-messaging>', 'Title': 'Post request with ajax for Firebase Cloud Messaging', 'ViewCount': 813, 'id': 51683959, 'user_AboutMe': None, 'user_AccountId': 13062270.0, 'user_DisplayName': 'Dev9977', 'user_Reputation': 1041.0, 'user_Views': 42.0, 'user_id': 9439037.0}}