I have been porting my code to PhantomJs and have been having a hard time with using $.ajax in Jquery. My code runs perfectly when the function call is dropped. I am new-ish to JavaScript other than in webpages. Here is my shortened code:
var page = require('webpage').create();
console.log('required webpage');
page.onConsoleMessage = function(msg) {
console.log(msg);
};
console.log('added message event handler')
page.open('', function() {
console.log('page.opened \nnow in page context');
page.render('opened.png');
page.includeJs("https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
page.evaluate(function() {
console.log('evaluating');
function getMessages() {
return new Promise((resolve, reject) => {
$.ajax({
url: 'https://www.example.com/myrequest.php',
type: 'POST',
data: {
mychar_id: 32,
offset: 0,
limit: 10
},
success: function(data) {
if (data.trim() === '') {
console.log("received null");
reject("Received null data");
} else {
resolve(data);
}
},
error: function(xhr, status, error) {
console.log('AJAX Error:', error);
reject(error);
}
});
});
}
getMessages().then(data => {
console.log(data);
}).catch(error => {console.error(error);});
console.log("extending jquery.");
// JQuery stuff goes here.
console.log('Final markup.')
console.log(document.body.innerHTML);
});
page.render('done.png')
console.log('exiting');
phantom.exit();
});
});
However, when running this with phantomjs, all I see is a blank cursor and the only way to exit is with ^c. I have tried everything I can think of, moving functions, etc. but so far, no luck. I know my function works elsewhere. What can I do?
jquery/1.6.1/