Ajax and ossn object not work

Malik Umer Farooq Posted in Technical Support 6 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
pk Malik Umer Farooq Replied 6 years ago

Followings

  • id tag in xml use for create sub dir by installer
  • Always see error log code not work
  • if project pre loaded labs don't load again
  • use external js for best performance
  • Use pre define function of project if have
  • understand codes of predefine functions before writing
  • Never use alert debuging js code special ajax
  • use console.log(); in js case
  • if we want get data from db only register action not page in ossn
  • & is not need in post method
  • Make friends with your programming environment, first
Indonesian Arsalan Shah Replied 6 years ago

To summarize this topic, Please tell me everything about what you have learned from this topic?

pk Malik Umer Farooq Replied 6 years ago

Yes we dont need add & sign
it much be params: "search=" + search,

Thanks you i got that so my code become good? logically
enter image description here

German Michael Zülsdorff Replied 6 years ago

Thanks for feedback, Malik.

Okay, so your code is finally working, but what seems to be still not quite clear is:

  1. Which way is your variable search passed to the action?
  2. Do we need that & in front of search or not?

So let's recall that final screenshot again:

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


What we already saw is that search has not become part of the request url. In fact it's not, because you made your variable part of a POST request. But how to verify that? Quite simply: Just scroll down in the same tab down to the bottom:

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


And you see your variable search appearing right here - together with the value 'how to' I entered in your input field.

The even more interesting part is (with still using your old code): The developer console displays "(empty)" one line before - as it would expect another variable here. But it's not there.
And of course it's not, because you coded: params: "&search=" + search,. So there is in fact no other var in front of search and we are getting this mysteroius 'empty' here.

As a little exercise you may add another var to your javascript like:

var anvar = 'Malik';

and change the params line to:

params: "another_var=" + anvar + "&search=" + search,

and see what appears in the developer console tab this time ...

Additionally it's time to learn the difference between a GET and a POST request. See https://www.w3schools.com/tags/ref_httpmethods.asp or other helpful information on the web.

pk Malik Umer Farooq Replied 6 years ago

> Don't you remember?

i remember

> If you have a look into Ossn core Javascript library again,

yes i see that already

> Then, why didn't I tell you how to use $ajax() correctly? Because the next learning step was:

thanks

> Don't use a low-level function if your programming

ok i understand

Thanks you so much

German Michael Zülsdorff Replied 6 years ago

> i am unsure why pure jquery ajax $.post and $,.get or ajax not work in
> ossn ? i use in my many project its work for me

Didn't we find the reason already in the very beginning?! It's not that $.post() wasn't working at all - but you were setting it up incorrectly. And that's why it returned a complete html page instead of the plain input data you were expecting. Don't you remember?

If you have a look into Ossn core Javascript library again, you'll note that both Ossn.PostRequest() and Ossn.ajaxRequest in fact end in a $.ajax() jquery call. In other words: It DOES work, but of course the parameters for a call like that have to be prepared thoughtfully.

Then, why didn't I tell you how to use $ajax() correctly? Because the next learning step was:

> Don't use a low-level function if your programming
> environment library already has a reliable high-level function accomplishing the same task

And that's the reason we were ending up with using Ossn.PostRequest() here.

pk Malik Umer Farooq Replied 6 years ago

yes sorry for delay i was so busy
i was say url look like that because first time i made typing mistake it show following error

jquery-1.11.1.min.js:4 POST http://localhost/ossn/action/Help/sedarch?ossn_ts=1513389855&ossn_token=6c42ef7099d6a022cd92f41817ef5082 404 (Not Found)

and url i click open new tab it show like that

http://localhost/ossn/action/Help/sedarch?ossn_ts=1513389855&ossn_token=6c42ef7099d6a022cd92f41817ef5082

thats reason i thunk i need add & sign

it work in both case i remove & sign it work fine
enter image description here

my js code?

        $(document).ready(function() {
    $("#search").keyup(function(){
        $("#control").css({"display":"none"});
        var search = $("#search").val();
    Ossn.PostRequest({
        url: Ossn.site_url + "action/Help/search",
        params: "search=" + search,
        callback: function(callback) {
            console.log("returned val is: " + callback["data"]);
        },
    }); 
});
});

if the code work why is there wrong in these code? maybe logic wrong
and i am unsure why pure jquery ajax $.post and $,.get or ajax not work in ossn ? i use in my many project its work for me

> One more question is for you why you choosen it as string params: "&search=" + search, ?

in core.php i find var $fdata = $data["params"];
i guess that it form data to be processed input fields maybe name&email and much more

in line 164 it show data: $fdata, thats means data send using $.ajax
and $fdata is the data of form input fields

if i wrong help me please
and thanks you so much sir @arslan and @zet

Indonesian Arsalan Shah Replied 6 years ago

Malik we are expecting your response

German Michael Zülsdorff Replied 6 years ago

Well,
maybe I wasn't emphasizing enough, that it's worth to have a look into the developer console not only when things are not working, but when things start working, too?!... ;)

Okay. Malik, you said:
"The url looks like that http://localhost/ossn/action/Help/sarch?ossn_ts=value&ossn_token=value .... because there are values already auto passed ..."

So far, that's correct, as Ossn is adding both security tokens automatically.

Next you said:
"so i need to add and & sign ..."

No bad idea at first sight, as you would expect the complete request to look like http://localhost/ossn/action/Help/sarch?ossn_ts=value&ossn_token=value&search=searchvalue then.

Good, time to open the developer console and verify whether it's really working that way ...

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


And - using the Header tab this time (or hovering the request on the left) we see:

No, your &search=value has NOT become part of your request url!!!

So why is your code working then ?

Indonesian Arsalan Shah Replied 6 years ago

@Malik,

I didn't said it didn't work but i said that is not required, again it seems you just copied code from OssnWall and without learning what actually it is. So please learn when someone teaches you. We are trying to teach you this since last two weeks.

One more question is for you why you choosen it as string params: "&search=" + search, ? You mey reply because i found this and i copied? Please find answer for it , its learning for you.

Don't copy codes if you copy please learn and find reason behind it. I am waiting to hear from you about my above question.

> And why the URL look like that I paste in previous post.

I am sorry i don't have answer for this that its you who decided to make it look like that. But in Ossn it didn't look like what you said.