简体   繁体   中英

jQuery Ajax POST JSON issue

I'm trying to pass a JavaScript array into a PHP script to then be put into a database but I'm having issues. I have used JSON.stringify on the array before sending. The request seems to work in that when stepping through the code debugging the php page is called but no data is passed into the POST global. I'm sure its something amateurish that I've missed but I'm struggling. This is the code:-

$.ajax({
    type: "POST",
    datatype: "json",
    url: "processdraw.php",
    data: {
        json: pDrawnTeams
    },
    contentType: "application/json; charset=utf-8",
    success: alert('worked')
})

If you are not getting any error in your javascript, make sure that you are getting that parameter like this in your php target file:

$myJSON = $_POST['json']; // ['parameter name']

after that you will need to decode that json

$myData = json_decode($myJSON, true) // true is for retrieving as an array

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