Tuesday, 15 January 2013

javascript - How to avoid 500 Internal Server Error if a JS session variable null -


I created this script to be read from different web sources. This script updates all the information (mail, post, friend) and details of their own drop-down window, as well as two side update times. I got this script my top manu Used in php page.

This script is working well for the user Login .

Now I pass the session ID variable to javascript var uid = ' Id; ? & Gt; '; So when a user is not logged in, my browser console displays 500 internal server error because there is no ID in the session, so this is the UID = ''

How do I remove this problem?

Here is my JavaScript script:

  var uid = '& lt;? Now $ session-> Id; ? & Gt; '; Function addmailno (type, msg) {// display mail / friend / something for post notifications} function addmailup (type, msg) {// something for display mail / post / friend drop-down} function addside (type , Msg) {// do something to show all friends / new posts in the side bar} Function waitFormailno () {$ .ajax ({type: "GET", url: "serverforupandside.php", cache: incorrect, async : False, data type: 'json', data: "uid =" + uid, timeout: 15000, success: work (data) {addmailno ("MyDivClass", data); Addmailup ("MyDivClass", Data); Addside "MyDivId", Eta); setTimeout (waitFormailno, 15000);}, error: function () {SetTimeout (Prticshaformel, 15000);}}); } $ (Document) .ready (function () {waitFormailno ();});  

serverforupandside.php

  & lt? Includes php ("db.php"); Include_once ("mysession.php"); While (true) {if ($ _ GET ['uid']) {global $ dbh; // All php queries are here after one // output data $ data = array (); $ Data ['upmail'] = $ upmail; $ Data ['upfollow'] = $ upfollow; $ Data ['uppost'] = $ uppost; // etc if all (! $ Data) empty (echo json_encode ($ data); Flush (); Mysqli_close ($ DBH); }} Mysqli_close ($ dbh); }? & Gt;  

Your JavaScript code should be modified:

  function waitFormailno () {$ .ajax ({type: "GET", url: "serverforupandside.php", cache: incorrect, async: false, datatype: 'json', data: UID = "+ UID, Timeout: 15000, Success: Function (Data) {Admelo (" MyDivClass ", Data); AdmAlux (" MyDivClash ", Data); Junk (" MyDevid ", Data);}}); } $ (Document) .ready (function () {setInterval (waitFormailno, 15000); // calls a function or evaluates an expression at specified intervals (in milliseconds)});  

PHP code:

  & lt ;? Includes php ("db.php"); Include_once ("mysession.php"); If (! ($ _ GET ['uid'])) {global $ dbh; // All php queries are here after one // output data $ data = array (); $ Data ['upmail'] = $ upmail; $ Data ['upfollow'] = $ upfollow; $ Data ['uppost'] = $ uppost; // etc if all (! $ Data) empty (echo json_encode ($ data); }}  

In addition, you should add assumptions before filling out the $ data array.


No comments:

Post a Comment