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.

0

3 Answers 3

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.

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
});
0

Add content-type in your ajax

    contentType: "application/json",

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.