Ajax and ossn object not work

Malik Umer Farooq Posted in Technical Support 7 years ago

I write simple ajax script but it does not work when i type in box page hang-up and reload everything gone to hide
Here is snapshot of mu ajax code
enter image description here

And i want search value i write search script it return all result
Here is snap
enter image description here

Help me please

Replies
German Michael Zülsdorff Replied 7 years ago

Honestly Malik,
I have not expected a question like that from you after all these days. I thought I have tought you using the developer console and debug Javascript?!?

Simply add

console.log('ts is: ' + ts);
console.log('token is: ' + token);

below the other log, and you will see: Both are undefined. And how could they? There ARE NO input fields named ossn_ts or ossn_token on that page jquery could get a value from. So yes: It IS WRONG.

You're still trying this and that out in the blue instead of consequently following my hints.
My last tip was trying to find any other occurrence of "$.post(Ossn.site_url" in Ossn code. So did you?

pk Malik Umer Farooq Replied 7 years ago

Is there anything wrong?

$(document).ready(function() {
    $('#search').keyup(function(){
        // alert('working');
        $('#control').css({'display':'none'});
        var search = $('#search').val();
        var ts = $('input[name=ossn_ts]').val();
        var token = $('input[name=ossn_token]').val();
        //alert(search);
        console.log('search val is: ' + search);
        $.post(Ossn.site_url+"action/Help/search",{'ossn_ts':ts,'ossn_token':token,action:search},function(data){
                //$('.search-result').html(data);
                // alert(data);
                console.log('search val is: ' + data);
            });
    }); 
});

its not work too

German Michael Zülsdorff Replied 7 years ago

Can this be true?
https://newblue.ongolito.net/action/helpdisk/add is working
and
https://newblue.ongolito.net/action/Help/search is not working?

There must be a difference, so let's examine ...

https://newblue.ongolito.net/demofiles/discussion-2675/Helpdisk-AddQuestion.png


First of all this is not Ajax, but a normal HTML submit.
But the more interesting part is: When pressing "Submit question" not only your 2 input fields (question and answer) will be submitted, but Ossn has automatically added 2 hidden input fields - holding a timestamp and a unique token, nobody won't know in advance. And these 2 hidden fields will be submitted, too.

Why that?

It's for security. Without these 2 values, anyone could manually do any post request from outside and really harm your site.

Both the timestamp and the token are missing in your Ajax post, that's why Ossn ignores your request and returns a 404 error. So the next question is: How to add them to your code?

What we know is: Ossn is using Ajax in Chat, Commments and other places. Thus it's time to remember https://www.opensource-socialnetwork.org/wiki/view/1137/how-to-find-something-a-word-pattern-in-the-source-code and find which way Arsalan is doing things like that.

So grab your unzipped Ossn source and search for: '$.post(Ossn.site_url'

pk Malik Umer Farooq Replied 7 years ago

if i do that its also show error
$.post(Ossn.site_url+"action/Help/search",{action:search},function(data){
404 not found

German Michael Zülsdorff Replied 7 years ago

Okay, so let's verify:
The way you registered your new action is correct.

But what about the way you are calling the action? You did

$.post(Ossn.site_url+"Help/search/",{action:search},function(data){

and actually this does NOT call your action! Instead, it is trying to call a PAGE again.
In your case localhost/Help/search/, and this page does not exist.

So, how would you call an action correctly? You can find it in your own code. See line 23 of
https://github.com/Lablnet/HelpDisk/blob/master/plugins/default/settings/administrator/helpdisk/settings.php

pk Malik Umer Farooq Replied 7 years ago

> So, please do the same and tell me whether you get a line in your error_log.

Error log file is empty i think ajax not access to this file so help me

German Michael Zülsdorff Replied 7 years ago

> but show same output as before

I have applied the same changes as you did. But my response looks different: This time, i don't even see what I entered into the search field!

To verify, I added a php error log statement to your helpsearch.php action. (see https://github.com/githubertus/Malik_Helpdisk/commit/7c83f9e332f201c9f4d2e8945e367dfc408d37d8)

But I don't see 'hello, this is helpsearch!' in my Ossn error_log file.

So, please do the same and tell me whether you get a line in your error_log.

pk Malik Umer Farooq Replied 7 years ago

> So registering a PAGE makes no sense here at all - we need an ACTION instead!

So i remove those line and add

ossn_register_action('Help/search', Helpdisk . 'actions/helpsearch.php');

in jquery

$.post(Ossn.site_url+'Help/search/',{action:search},function(data){

but show same output as before also i create helpsearch file inside actions folder
https://github.com/Lablnet/HelpDisk/

German Michael Zülsdorff Replied 7 years ago

Okay,
I have applied your changes to my site now. Let's go right into it and have a look at the response again:
https://newblue.ongolito.net/demofiles/discussion-2675/HelpDisk-Helpsearch.png


In fact the "How to" I entered to your field appears on the new search page. But deep down on line 259! So why are we getting all that unwanted stuff before that line? Because YOU programmed it that way. :)
Have a look at your com file: You have told Ossn to register a PAGE - and Ossn does what you programmed: It displays a page. (You even gave it a title!) That's why we are getting all those lines before and below line 259.

> So let's recall what you intentionally wanted: You wanted just some data
> from the db - not a page.

So registering a PAGE makes no sense here at all - we need an ACTION instead!

pk Malik Umer Farooq Replied 7 years ago

> Never ever use the alert box to debug Ajax!

Done always console.log()

>that is not what you wanted. So what's your idea to proceed?

I want make real time search facilities for question search

at a movement ago my code

    $(document).ready(function() {
    $('#search').keyup(function(){
        // alert('working');
        $('#control').css({'display':'none'});
        var search = $('#search').val();
        //alert(search);
        console.log('search val is: ' + search);
        $.post(Ossn.site_url+'/Help/',{action:search},function(data){
                //$('.search-result').html(data);
                // alert(data);
            });
    }); 
});

I change to

$(document).ready(function() {
$('#search').keyup(function(){
    // alert('working');
    $('#control').css({'display':'none'});
    var search = $('#search').val();
    //alert(search);
    console.log('search val is: ' + search);
    $.post(Ossn.site_url+'/helpsearch/',{action:search},function(data){
            //$('.search-result').html(data);
            // alert(data);
        });
}); 

});

Same result as before so howto/ fix that i dont know why it return such response So very special thanks you again but more help is need

Premium Version

Due to the many requests in the past for additonal features and components we have decided to develope a premium version. Features like Hashtags, Videos, Polls, Events, Stories, Link Preview, etc included in it.

$199 (Life Time)
Learn More

Other Questions