2

We want to post a json object to PHP, this is what we currently have:

var sitePersonel = {};
var userData = [];

sitePersonel.userData = userData;
var userData = {
    "userId": username,
    "name": name,
    "artists": artistNames
};

sitePersonel.userData.push(userData);
console.log(sitePersonel);

var jsonpArray = JSON.stringify(sitePersonel);

function phpCallback() {
}

$.ajax({
    type: "POST",
    dataType: "jsonp",
    url: "http://student.cmd.hro.nl/0879644/spotify/data.php",
    data: { myData: jsonpArray },
    success: function (data) {
        alert('Items added');
    },
    jsonpCallback: phpCallback(),
    error: function (e) {
        console.log(e.message);
    }

We are getting an error, on loading this jQuery. We have tried debugging, but onfortunately, we aren't getting far. Our console gives the following error:

Resource interpreted as Script but transferred with MIME type text/html We do land into the phpCallback(); but the console is giving us errors and PHP isn't working either.

We want to send the JSON object to PHP to save into a database. We are working on a Spotify App.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339

3 Answers3

0

Try changing content type to text/javascript

JSONP request: "Resource interpreted as Script but transferred with MIME type text/html"

Hope this solves your problem.

Community
  • 1
  • 1
abhinsit
  • 3,214
  • 4
  • 21
  • 26
0

plain example, modify to your own case (i.e. dataType from json to jsonp)

jQuery.ajax({
    url: url,
    type: "POST",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: data,
    success: callback
});
ROLO
  • 581
  • 4
  • 13
0

Add content-type in your ajax

    contentType: "application/json",

By default it is 'application/x-www-form-urlencoded; charset=UTF-8'

Shijin TR
  • 7,516
  • 10
  • 55
  • 122