AJAX Jquery request error - javascript

I'm getting a "Uncaught SyntaxError: Unexpected end of input" error, when i take out the below snippet the error goes away. Maybe its because ive been up all night but i cant for the life of me find the issue. Can anyone please shed some light?
$.ajax({
url: "scripts/getWorkerPendingJobs.php",
type: "get",
data: {uid: userId},
success: function(dataa) {
var daa = JSON.parse(dataa);
var amtt = parseInt(daa.length);
var htmll = "";
if (amtt > 0) {
$.each(daa, function(indexx) {
var status;
var acc = daa[indexx].accepted.toString();
if (acc == 1) {
status = "Pending";
}
if (acc == 2) {
status = "Accepted";
}
htmll += '<li><img src="./noimage.png" /><h3>' + daa[indexx].dateadded + ' | ' + daa[indexx].shortdesc + '</h3><h2>Status : ' + status + '</h2><p>' + daa[indexx].description + '</p></li>';
$('.worker-applied-jobs').html(htmll).promise().done(function() {
$(this).listview("refresh");
});
});
} else {
$('.worker-applied-jobs').html('<p style="margin-left:10px;">You have not applied for any jobs.</p>');
}
}
});

Related

How to use the instagram api?

I have a problem since few minutes, my code worked perfectly and for a few minutes I have a problem, i have this message error :
main.js:6 Uncaught TypeError: Cannot read property 'user' of undefined
my JS code :
function getInstagram(id) {
$.ajax({
url: 'https://www.instagram.com/' + id + '?__a=1',
type: 'get',
success: function (response) {
posts = response.graphql.user.edge_owner_to_timeline_media.edges;
posts_html = '';
is_private = response.graphql.user.is_private;
if (is_private === true) {
$("#private").css("display", "none");
} else {
$("#private").css("display", "block");
}
if (Array.isArray(posts) && posts.length) {
for (var i = 0; i < 4; i++) {
url = posts[i].node.display_url;
posts_html += '' +
'<div class="col-md-3">' +
'<img style="max-height:200%; width:100%; cursor: pointer" src="' + url + '" onclick="onClick(this)">' +
'</div></div>';
}
}
$(".posts").html(posts_html);
},
error: function (xhr) {
if (xhr.status === 404) {
$("#private").css("display", "none");
}
},
});
}
I tried to look at the request on Postman and i can see they ask for login but normaly i dont need to looking to get all the pics from a public instagram

Unexpected identifier on JSON Eval function at Object.success

i got error on the line eval it says this is na error eval('result = ' + data.d);
this is my json format. i want may data to set pagination limit in 5 per page but when i increase the data. its show all. i want to limit the data per 5 data per page there is variable pagesize = 5 but its not working because of the eval() error
{
"d": [
{"FChaOnline":"0","FChaName":"Testing10"},
{"FChaOnline":"1","FChaName":"Testing1"},
{"FChaOnline":"0","FChaName":"Testing28"}
]
}
Uncaught SyntaxError: Unexpected identifier
at Object.success (JRGMain.js:191)
at Object.resolveWith (jquery-1.5.1.js:862)
at done (jquery-1.5.1.js:6591)
at XMLHttpRequest.callback (jquery-1.5.1.js:7382)
$.ajax({
type: 'post',
dataType: 'json',
contentType: "application/json; charset=utf-8",
url: './WebService/GetUserFriendList',
data: '{"PageNo" : "' + PageNo + '", "PageSize" : "5", "UserID" : "' + UserID + '"}',
success: function(data) {
var result, TotalItemCount, list;
eval('result = ' + data.d);
TotalItemCount = result.TotalItemCount;
list = result.List;
PageCount = Math.ceil(TotalItemCount / 5);
var html = '';
if (list.length > 5) {
$.each(list, function(index, obj) {
if (obj.FChaOnline == '0') {
html += '<li class="off">' + obj.FChaName + '<span class="state"><img src="./Images/Icon/off_icon.gif" alt="on"></span></li>';
} else {
html += '<li class="on">' + obj.FChaName + '<span class="state"><img src="./Images/Icon/on_icon.gif" alt="on"></span></li>';
}
});
} else {
html = '<li class="nothing">There are no friends registered.</li>';
}
$('#relation-list').html(html);
$('#page-no').text(PageNo);
$('#total-page').text(PageCount);
},
error: function(data) {
alert(data.status + ' : ' + data.statusText + ' : ' + data.responseText);
}
});

Ajax callback from two functions

Hello i have a problem with ajax and two callbacks code look like that
loadTasks(function(tasks) {
taskhtml = whatStatus(tasks);
loadSupportList(function(tasks) {
supporthtml = support(tasks);
$("#devtask-table").html(
titleDraw() + "<tbody>" + supporthtml + taskhtml
+ "</tbody>");
hideCurtain();
});
});
And it's work fine when loadSupportList or loadTasks have records. But when one of them don't i get Error, status = parsererror, error thrown: SyntaxError: Unexpected end of input. And it jump off function and leave me with nothing. My ajax function looks that way :
function loadSupportList(callback) {
$.ajax({
type : "POST",
url : url,
data : {
userId : userId,
sessionId : sessionId
},
success : function(data) {
callback(data['tasks']);
},
error : function(jqXHR, textStatus, errorThrown) {
console.log("Error, status = " + textStatus + ", " + "error thrown: "
+ errorThrown);
}
});
}
And i dont know what to change, to ignore(?) or made some json with ('success')? Is there a way? Thanks for your time.
I make something silly, but its work so it's not silly
loadTasks(function(tasks) {
taskhtml = whatStatus(tasks);
$("#devtask-table").html(
titleDraw() + "<tbody>" + supporthtml + taskhtml
+ "</tbody>");
loadSupportList(function(tasks) {
supporthtml = support(tasks);
$("#devtask-table").html(
titleDraw() + "<tbody>" + supporthtml + taskhtml
+ "</tbody>");
hideCurtain();
});
});
assuming tasks returns an array... update your loadSupportList function like this..
success : function(data) {
var ret = [];
if(
'object' == typeof data &&
'undefined' != typeof data.tasks
) ret = data.tasks;
callback(ret);
}

"Page Unresponsive" Error in Chrome when trying to GET files > 500kb

I am trying to get Log files in my application, and am using Ajax to do so.
The problem is that while using Chrome I constantly get 'Page Unresponsive' Errors when I try to load the files.
My application is working fine in Firefox, so I'm guessing this is a webkit issue.
I removed xhr because I heard chrome has a problem with it.
This is my code -
$.ajax(
{
type: 'GET',
url: "Logs/GetLogFile?option=" + logFile,
data: {},
success: function (data) {
var fileData = data;
cleanUp();
currentlyViewedFile = logFile;
var fileLines = fileData.split('\n');
var htmlString = '';
var lineCount = 0;
for (var i = 0; i < fileLines.length; i++) {
lineCount = lineCount + 1;
if (i == (highlightLine - 1)) {
htmlString += '<span id="highLoc" style="display:block"><font size="4" color="red">' + lineCount + '. ' + fileLines[i] + '</font></span>';
}
else {
htmlString += '<span class="spanClass">' + lineCount + '. ' + fileLines[i];
}
var combinedLine = fileLines[i];
var isNewLogLine = false;
var newLogLineCount = 0;
while (((i + newLogLineCount + 1) < fileLines.length) && (isNewLogLine == false)) {
var NextlogLine = fileLines[i + newLogLineCount + 1].split(' ');
isNewLogLine = isLogLine(NextlogLine[0]);
if (isNewLogLine == true) {
break;
}
htmlString += fileLines[i + newLogLineCount + 1];
combinedLine += fileLines[i + newLogLineCount + 1];
newLogLineCount = newLogLineCount + 1
}
i = i + newLogLineCount;
var logLine = combinedLine.split(' ');
var logMsgStartIndex = combinedLine.indexOf(logLine[5]);
var logMsg = combinedLine.substring(logMsgStartIndex);
var obj = {
lineNos: lineCount,
date: logLine[0],
time: logLine[1],
severity: logLine[2],
logClassName: logLine[4],
logMessage: logMsg
};
fileArray.push(obj);
if ((i % 1000) == 0) {
$("#textPlace").append(htmlString);
htmlString = '';
window.scrollTo(0, 2);
}
}
fileLines = null;
$("#fetchProgress").hide();
$("#textPlace").append(htmlString);
//highlight error location
var scrollX = $("#highLoc");
if (scrollX) {
if (!scrollX[0]) return;
var scrollDistance = '{ scrollTop: ' + scrollX[0].offsetTop + '}';
var distanceInJson = eval("(" + scrollDistance + ")");
$("#mainContentBox").animate(distanceInJson, 1);
}
},
///success function end
fail: function (jqXHR, textStatus) {
alert("Request failed: " + textStatus);
}
});
If anyone has any ideas on how to deal with this issue, please let me know.

Jquery Ajax Call always returns error

I have a javascript code as below
var xReg = '<region><width>' + $("#txtWidth").val() + '</width> <height>' + $("#txtHeight").val() + '</height><float>' + $("#floats").val() + '</float><contenttype>' + $("#contenttype").val() + '</contenttype><style>' + rgnStyle + '</style></region>';
$.ajax({
type: "POST",
url: "myurl/addRegion",
data: "{pubId: '" + Number($("#pubs").val()) + "',section: '" + $("#sections option:selected").text() + "',layoutW: '" + Number($("#txtLayoutW").val()) + "',layoutH: '" + Number($("#txtLayoutH").val()) + "',bSubReg: '" + Boolean($("#chkSubRegion").is(':checked')) + "',parentRegId: '" + Number(parentRgn) + "',sXmlRegion: '" + xReg.toString() + "'}",
contentType: "application/json; charset=utf-8",
async: false,
dataType: "json",
success: function (result) {
document.body.style.cursor = 'pointer';
if (result.d == -1) {
$("#errMsg").text("Failed to create new region");
}
else {
if ($("#chkSubRegion").is(':checked')) {
$("#regions").append("<option class='ddindent' value='" + result.d + "'>REGION-" + result.d.toString() + "</option>");
} else {
$("#subregions").append("<option class='ddindent' value='" + result.d + "'>SUBREGION-" + result.d.toString() + "</option>");
}
}
},
error: function (result) {
if (result.status == 200 && result.statusText == 'OK') {
if ($("#chkSubRegion").is(':checked')) {
$("#regions").append("<option class='ddindent' value='" + result.d + "'>REGION-" + result.d.toString() + "</option>");
} else {
$("#subregions").append("<option class='ddindent' value='" + result.d + "'>SUBREGION-" + result.d.toString() + "</option>");
}
}
else {
alert("FAILED : " + result.status + ' ' + result.statusText);
}
},
async: true
});
Server code as below
[WebMethod]
public int addRegion(int pubId, string section, int layoutW, int layoutH, bool bSubReg, int parentRegId, string sXmlRegion)
{
string path = Server.MapPath("~");
path = Path.Combine(path, "Published");
path = Path.Combine(path, pubId.ToString());
path = Path.Combine(path, section);
XmlDocument doc = new XmlDocument();
int rgnCount = 0;
try
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path = Path.Combine(path, "layout.xml");
if (!File.Exists(path))
{
XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(docNode);
XmlNode templateNode = doc.CreateElement("layout");
doc.AppendChild(templateNode);
XmlNode xNodeW = doc.CreateElement("width");
xNodeW.AppendChild(doc.CreateTextNode(layoutW.ToString()));
XmlNode xNodeH = doc.CreateElement("height");
xNodeH.AppendChild(doc.CreateTextNode(layoutH.ToString()));
}
else
{
doc.Load(path);
doc.DocumentElement.SelectSingleNode("/layout/width").InnerText = layoutW.ToString();
doc.DocumentElement.SelectSingleNode("/layout/height").InnerText = layoutH.ToString();
}
XmlElement root = doc.DocumentElement;
XmlNode xParent = root;
if (bSubReg)
{
xParent = root.SelectSingleNode("/layout/region[id='" + parentRegId.ToString() + "']");
rgnCount = xParent.SelectNodes("/region").Count;
}
else
{
rgnCount = root.SelectNodes("/Layout/region").Count;
}
rgnCount++;
XmlDocumentFragment docFragment = doc.CreateDocumentFragment();
docFragment.InnerXml = sXmlRegion;
XmlNode xID = doc.CreateElement("id");
xID.AppendChild(doc.CreateTextNode(rgnCount.ToString()));
docFragment.FirstChild.AppendChild(xID);
xParent.AppendChild(docFragment);
doc.Save(path);
return rgnCount;
}
catch (Exception eX)
{
return -1;
}
}
The call is going to server from client. And no issues I could find in server code till last return statement while I debugged it. In the javascript debugging I found that after ajax call is always going to error callback function. Can anyone suggest whats wrong with the code.
Thanks & Appreciate Your Time.
I found the bug in my code there is issue with below lines of code
},
async: true
at the end of error: callback function
I removed the line async: true and it worked

Categories

Resources