简体   繁体   中英

Post json data to PHP

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.

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.

plain example, modify to your own case (ie dataType from json to jsonp)

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

Add content-type in your ajax

    contentType: "application/json",

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM