Home  >  Article  >  Backend Development  >  javascript - What is the role of name in input?

javascript - What is the role of name in input?

WBOY
WBOYOriginal
2016-08-04 09:20:001201browse

<code>$.post(
    url : 'xxx',
    data : "{x:var1,y:var2}",
    function(msg){
        alert(data);
    },
    type:'json'
)</code>

I use $post similar to what I wrote above to pass parameters to the java backend, where var1 and var2 are the contents of the input space that I obtained using $("#xx").val(). They were successfully obtained on the front end. But the backend cannot obtain it correctly (the input space has no name attribute)
My question is: In this case of using $.post() without form and submit, what effect does the name attribute in the input control have on the transfer of data? No effect?

Reply content:

<code>$.post(
    url : 'xxx',
    data : "{x:var1,y:var2}",
    function(msg){
        alert(data);
    },
    type:'json'
)</code>

I use $post similar to what I wrote above to pass parameters to the java backend, where var1 and var2 are the contents of the input space that I obtained using $("#xx").val(). They were successfully obtained on the front end. But the backend cannot obtain it correctly (the input space has no name attribute)
My question is: In this case of using $.post() without form and submit, what effect does the name attribute in the input control have on the transfer of data? No effect?

There is no impact. This is an ajax post request. The name and value of the data have been specified in the data. When fetching it in the background, the data named x and the name y can be obtained respectively. var1 Got with var2

It doesn’t matter whether there is a form or submit, as long as you spell the things in the data correctly. There is already a FormData object in XHR. It is just as easy to fill in the new object and send it out as data.
But is it okay for you to write like this? I usually write strings like 'key=value&key2=value2', or use json {'key1':'value1','key2':'value2'}, which is relatively safer...

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn