I am having a bit of an issue with my code and I just can't pinpoint what it is. I have the following function:
var settings = {
dataType: 'text',
"url": "https://example.com/v1/" + <?php echo $_POST["player"]; ?> + "?limit=1",
"method": "GET",
}
This returns my JSON in a text format that I can run REGEX on. Here I run my regex:
$.ajax(settings).done(function (response) {
extracted = response.match(/(?=\[\{).*(?:\}\])/);
itm = <?php echo $_POST["player"]; ?>;
alert(extracted)
The alert is exactly the data I need and alerts just fine. My problem lies when it comes to saving. Here is my code:
$.ajax({
type: "post",
url: 'post.php',
data: {json: extracted, itemid: itm},
success: function(){
window.location.href = "/check/";
},
});
});
For whatever reason it only saves as a blank json file. But if I remove the regex it save just fine to my server. Am I missing something here? I feel like I have tried many things over the last 2 hours and I am at the last resort. The fact that it is JSON shouldn't matter. Even if I can save 'extracted' as text I would be happy.
If it makes a difference, here is my PHP below that saves the file.
<?php
if(isset($_POST['json']))
{
$data=$_POST['json'];
$itemid=$_POST['itemid'];
$fp = fopen($itemid.'.json', 'w');
fwrite($fp, $data);
fclose($fp);
}
?>