jquery $.GET parameters passing in the url

孤者浪人 提交于 2019-11-30 17:01:49

问题


this is a general-purpose way to make GET requests with Jquery:

var loadUrl="mypage.php";
$("#get").click(function(){   
    $("#result").html(ajax_load);   
    $.get(   
        loadUrl,   
        {language: "php", version: 5},   
        function(responseText){   
            $("#result").html(responseText);   
        },   
        "html"  
    );   
});  

I was wondering if I could pass parameters (Ex.language and version) directly in the Url(after urlencoding them):

var loadUrl="mypage.php?language=php&version=5";
$("#get").click(function(){   
    $("#result").html(ajax_load);   
    $.get(   
        loadUrl,      
        function(responseText){   
            $("#result").html(responseText);   
        },   
        "html"  
    );   
});  

Is that possible? And anyhow wich is the cleanest solution to make an ajax call if I have all of the parameters I need urlencoded (Ex.rate me)


回答1:


Yes that is possible but you can also do it this way.

$.get(
   "mypage.php", 
   { version: "5", language: "php" }, // put your parameters here
   function(responseText){
      console.log(responseText);
   },
   'html'
);



回答2:


$.get(

  url: url,    //your url eg. mypage.php

  data: data,   // Parameter you want to pass eg. {version:"5" , language : "php"}

  success: callback // function after success

);

follow the below link

http://api.jquery.com/jQuery.getJSON/



来源:https://stackoverflow.com/questions/5383222/jquery-get-parameters-passing-in-the-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!