How to pass php functions to js

Source: Internet
Author: User
How to pass php functions to js there are some string processing functions in the backend php using projects, or the string validity check class
The front-end of the web page also needs to be used, and it is difficult to maintain without writing js code separately. There may also be small differences that may lead to new problems.
My idea is to write a unified interface on both js and php, define a set of function call rules, how to transmit function names and parameters, and so on. return results in json format.
Have these questions:
1. I have heard of the word webservice. does it refer to the design idea or the actual php module?
2. how can we ensure the security and privacy of this network-based function call method? I just want to be called by my project
3. is there a better solution to this problem due to network latency?
4. can nodejs truly implement code sharing between the frontend and backend? Can I add an example?


Reply to discussion (solution)

1. the definition of webservice is actually very wide. it is actually a remote call technology.
2. based on identity authentication, the backend and front-end interaction of the same domain can be based on the user identity authentication of the website. There are many ways to implement the interaction verification between the backend and the back-end.
3. simple functions such as string processing (not complex services based on data queries) should also be implemented in js, and network problems cannot be solved.
4. web-based solutions are impossible.

In fact, the asp.net control has already been implemented by the landlord.

Compile a unified interface on both js and php to define a set of function call rules, how to transmit function names and parameters, and return results in json format.

? Use ?? Yes? 1,? Query generation ?? Yes ?? Js and php? Set

This is the case!
The front-end transmits sensitive data, and the server completes processing and returns json data!
However, it is recommended that you do not store string processing on the server, which increases the pressure on the server! Try to process it on the client!

Php functions cannot be used in JavaScript. The reason is simple: the syntax rules of the two are different.
Therefore, as long as the syntax rules of the two are the same, they can be called each other, such as nodejs and javascript.
Of course, because the two are at both ends of the network, there is a communication problem. Fortunately, nodejs naturally supports the websock protocol, and communication problems are hidden internally.
However, this is obviously not a problem discussed in the php layout.

Why is Microsoft always pushing? Client B/S, so the client control is displayed in vs (you only need to write code on the server)
It actually implements communication between the browser and the server through the xmlhttp component.
This kind of technology is the popular AJAX technology.

Webservice is a WEB service.
The strange thing is why we should list it separately. Isn't a common website a WEB service?
If http: // localhost/add. php? A = 1 & B = 2 returns 3, which is called a webpage.
So http: // localhost/index. php? M = add & a = 1 & B = 2 also returns 3. Why is it called remote call?
Only the HTTP get and post methods are called webpages.
Other HTTP methods such as put, delete... are called REST.
In fact, there is no need to struggle with some terms, as long as the goal can be achieved.

Php currently provides
Initial stage of XML-PRC webservice
Advanced stage of SOAP webservice

However, due to the popularity of json, these things are not fragrant.
However, since both. Net and Java have refused to provide official JSON operations, they cannot exit the stage of history.

1. the definition of webservice is actually very wide. it is actually a remote call technology.
2. based on identity authentication, the backend and front-end interaction of the same domain can be based on the user identity authentication of the website. There are many ways to implement the interaction verification between the backend and the back-end.
3. simple functions such as string processing (not complex services based on data queries) should also be implemented in js, and network problems cannot be solved.
4. web-based solutions are impossible.

In fact, the asp.net control has already been implemented by the landlord.



2. we hope to get more detailed technical implementation details to prevent off-site calls.
3. try to solve the problem ~ The reason why I don't want to implement js is as mentioned in the main building. I personally don't like to write two pieces of code for the same function, which is so awkward.
4. the biggest obstacle to solving this problem is that php and js have no syntax, but nodejs does not. so, the latter is the most likely solution.
I have never touched asp.net. I don't know how far it is implemented?

Compile a unified interface on both js and php to define a set of function call rules, how to transmit function names and parameters, and return results in json format.

? Use ?? Yes? 1,? Query generation ?? Yes ?? Js and php? Set



Logically speaking, I think it can be shared: pass in a string to determine whether it is a mailbox or not, and return a Boolean value, which can be used no matter whether it is a front or back end.

This is the case!
The front-end transmits sensitive data, and the server completes processing and returns json data!
However, it is recommended that you do not store string processing on the server, which increases the pressure on the server! Try to process it on the client!



Well, the server pressure is indeed a wake-up.
So I want to find a better way to automatically port some functions of php to js without occupying additional requests.

After so many projects, there will be no more code for this type of simple verification, and there will also be a dedicated library for client verification. there will be no much code to write, and there will not be much repeated code.
For example, if the user name does not exist, the client cannot implement it on its own, so you don't have to worry about it.

So I want to find a better way to automatically port some functions of php to js without occupying additional requests.



Unless you can develop a compiler

Php functions cannot be used in JavaScript. The reason is simple: the syntax rules of the two are different.
Therefore, as long as the syntax rules of the two are the same, they can be called each other, such as nodejs and javascript.
Of course, because the two are at both ends of the network, there is a communication problem. Fortunately, nodejs naturally supports the websock protocol, and communication problems are hidden internally.
However, this is obviously not a problem discussed in the php layout.

Why is Microsoft always pushing? Client B/S, so the client control is displayed in vs (you only need to write code on the server)
It actually implements communication between the browser and the server through the xmlhttp component.
This kind of technology is the popular AJAX technology.

Webservice is a WEB service.
The strange thing is why we should list it separately. Isn't a common website a WEB service?
If http: // localhost/add. php? A = 1 & B = 2 returns 3, which is called a webpage.
So http: // localhost/index. php? M = add & a = 1 & B = 2 also returns 3. Why is it called remote call?
Only the HTTP get and post methods are called webpages.
Other HTTP methods such as put, delete... are called REST.
In fact, there is no need to struggle with some terms, as long as the goal can be achieved.

Php currently provides
Initial stage of XML-PRC webservice
Advanced stage of SOAP webservice

However, due to the popularity of json, these things are not fragrant.
However, since both. Net and Java have refused to provide official JSON operations, they cannot exit the stage of history.



Thank you, xu. I have learned a lot.

I'm very interested in why Node. js is booming. why is it so sudden?
Many people say the biggest reason is javascript, which makes it easy for front-end engineers to turn back
I think that since they are a family member, it is not necessary for network communication to implement point String Processing. it can be done directly on the local machine.
For example, if a function. js file is requested in html, the native back-end code of nodejs is actually directly copied in.
Although it is not the current version, I would like to know

I encountered the word "soap" several times when I was working on a third-party interface.
Because of the existence of the sdk, the general principle can be determined, so I didn't check it. it was originally a set of standardized things ~
My colleague from the ios app asked me, are you using webservice in the background?
I said no, just output a json string like a normal webpage.

The main selling point of nodejs is non-blocking I/O.
To say that. net java does not operate on json, php only has json_encode and json_decode.

Php js? Different methods
So? Generation? Yes ???? .

If you submit it to the server every time? Judge ?, Server? ? The power is huge.

After so many projects, there will be no more code for this type of simple verification, and there will also be a dedicated library for client verification. there will be no much code to write, and there will not be much repeated code.
For example, if the user name does not exist, the client cannot implement it on its own, so you don't have to worry about it.



What you said makes sense, but I am thinking about more possibilities: there is still a lot of space for upgrading web development.

There is a possibility, but it should not be limited to code replication...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.