Mahdi_Nine
Mahdi_Nine

Reputation: 14761

return null with query string whtn i use ajax-jquery

this is my jquery code:

<script type="text/javascript">
    $(document).ready(function () {
        $("a").hover(function () {

            $.ajax({
                type: "POST",
                url:"Dbread.aspx",

                data: "name=salam",
                success: function (result) {
                    alert(result);


                } //end of success




            });


        }, function () {



        });


    });

</script>

in Dbread.aspx i wrote:

      string str= Request.QueryString["name"];
    Response.Write(str);

but the problem is that query string result is null all times!!! what is problem?

Upvotes: 1

Views: 959

Answers (3)

dnxit
dnxit

Reputation: 7350

extra space can cause this problem

PrintPreview.NavigateUrl = "~/ReportViewer.aspx?ReviewID=" + Session["ArticleID"].ToString() + " & User=" + (Session["ID"]).ToString();

above is not working it returns null in ID

but in the below example it works fine just change of space character

PrintPreview.NavigateUrl = "~/ReportViewer.aspx?ReviewID= " + Session["ArticleID"].ToString() + "&User=" + (Session["ID"]).ToString();

http://afzal-gujrat.blogspot.com/

Upvotes: 0

Baz1nga
Baz1nga

Reputation: 15579

This is the best practice.

$.ajax({
    type: "POST",
    url:"Dbread.aspx",
    data: {name:"salam",qs:"bla"},  //also i appended the qs parameter
    success: function (result) {
        alert(result);

    } //end of success
});

Upvotes: 1

Andrei
Andrei

Reputation: 56716

You are using method "POST" and not providing actual query string. Query string is created only for "GET" method and should be the part of the URL, so the fix would be:

$.ajax({
    type: "GET",
    url:"Dbread.aspx?name=salam",
    ...

or

$.ajax({
    type: "GET",
    url:"Dbread.aspx",
    data: "name=salam",
    ...

Upvotes: 0

Related Questions