Best Jeanist
Best Jeanist

Reputation: 1129

Error when connect to server AJAX call

I have this code below i'm trying to send a data to a server but the only documentation i have to connect to it is a sample data post to the server it looks like this {“method”:“fruit-post”,“data”:{“red-fruits”:["Apple","Cherry","Tomato"],“green-fruits”:["Grapes","Watermelon"],“yellow-fruits”:["Lemon","Pear"],“purple-fruits”:[]}} Am i doing anything wrong here for my code below becuase i keep getting an Error 405 and Response for preflight has invalid HTTP status code 405. Any help would be greatly appreciated

var fruitData = {};

var key = "red-fruits";
fruitData[key] = red;

var key2 = "green-fruits";
fruitData[key2] = green;

var key3 = "yellow-fruits";
fruitData[key3] = yellow;

var key4 = "purple-fruits";
fruitData[key4] = purple;

var fruitString = JSON.stringify(fruitString);

$.ajax({
    method: "fruit-post", 
    type: "POST",
    url: "LINK",
    data: {
        "data": fruitString
    },
    cache: false,
    success: function(data) {
}

Upvotes: 0

Views: 76

Answers (2)

BraveButter
BraveButter

Reputation: 1458

Your ajax request is wrong. Look here http://api.jquery.com/jquery.ajax/

method has to be POST,GET or PUT

$.ajax({
  url: "LINK",
  method: "POST",
  data: {
        “method”:“fruit-post”,
        “data”:{
            “red-fruits”:["Apple","Cherry","Tomato"],
            “green-fruits”:["Grapes","Watermelon"],
            “yellow-fruits”:["Lemon","Pear"],
            “purple-fruits”:[]
        }
  },
  dataType: "html",
  cache: false,
      success: function(data) {
  }
});

Upvotes: 1

Nikhil Ghuse
Nikhil Ghuse

Reputation: 1288

ajax uses method type:'get' or "post" no need of method mention you method in url

 $.ajax({
    url: "LINK",
    type: "POST",
    data: {
         "data": fruitString
    },
    dataType: "html",
    cache: false,
    success: function(data) {
  }
 });

Upvotes: 0

Related Questions