简体   繁体   English

将JSON数据发布到PHP

[英]Post json data to PHP

We want to post a json object to PHP, this is what we currently have: 我们想要将一个json对象发布到PHP,这是我们目前拥有的:

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. 加载此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(); Resource interpreted as Script but transferred with MIME type text/html我们确实将其phpCallback(); but the console is giving us errors and PHP isn't working either. 但是控制台给我们带来了错误,PHP也不起作用。

We want to send the JSON object to PHP to save into a database. 我们希望将JSON对象发送到PHP以保存到数据库中。 We are working on a Spotify App. 我们正在开发Spotify应用。

Try changing content type to text/javascript 尝试将内容类型更改为text / javascript

JSONP request: "Resource interpreted as Script but transferred with MIME type text/html" JSONP请求:“资源被解释为脚本,但以MIME类型text / html传输”

Hope this solves your problem. 希望这能解决您的问题。

plain example, modify to your own case (ie dataType from json to jsonp) 一个简单的示例,修改为您自己的情况(即,从json到jsonp的dataType)

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

Add content-type in your ajax 在您的ajax中添加content-type

    contentType: "application/json",

By default it is 'application/x-www-form-urlencoded; charset=UTF-8' 默认情况下为'application/x-www-form-urlencoded; charset=UTF-8' 'application/x-www-form-urlencoded; charset=UTF-8'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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