views:

33

answers:

2

Hello all,

I'm trying to send some data in an Ajax call to update a record in the server

 var OrderNotes = $.ajax({
                 url:'AjaxActions/OrderNotesUpdate.aspx?OrderID=' + OrderID + '&Notes=' + $('#txtNotes').val(),
                 async:false                 
               }).responseText;

"Notes" are in unicode.

When i'm checking the querystring on the receiving page i'm getting not getting the same code (not the text i'v enterd).

Any one knows any thing about it? is it becouse the data is from an asp.net textbox? what can i do about it?

p.s before sending i checked and every things is as it should, just in the querystring every thing going wrong... 10x

+3  A: 
jQuery.ajax({
            url:'AjaxActions/OrderNotesUpdate.aspx',
            data:{
                 OrderID:OrderID,
                 Notes:$('#txtNotes').val()
                 },
           async:false,
           type:'get',
           success:function(data)
                   {
                    //do something here
                    }
})
Praveen Prasad
10x, thats great....:-) i can't eccept the answer for 6 minutes....so i'll wait, but 10x
Erez
+1  A: 

First of all the answer of Praveen Prasad I find correct. I want only to add a little description which will be answeron the question "Why ... ?" and not "How ... ?".

If parameter which you send to the server per HTTP GET has some special characters then there can not be used in URL without encoding, so you have to use at least

url:'AjaxActions/OrderNotesUpdate.aspx?OrderID=' +
    encodeURIComponent(OrderID) + '&Notes=' + encodeURIComponent($('#txtNotes').val())

Next step: you can use jQuery.param() to encode URL parameters with respect of encodeURIComponent and place '&' character between paramters:

$.ajax({
    url:'AjaxActions/OrderNotesUpdate.aspx?' +
        $.param({OrderID: OrderID, Notes: $('#txtNotes').val()}),
    async:false})

or

$.ajax({
    url:'AjaxActions/OrderNotesUpdate.aspx' +
    data: { OrderID: OrderID, Notes: $('#txtNotes').val()},
    async:false})

which place '?' between url and data encoded by $.param if url don't already contain '?' otherwise it use '&' instead.

Next: you should try to use asynchrone version of $.ajax whenever it is possible. One needs see more parts of your code to help you. In general it should be

$.ajax({
    url:'AjaxActions/OrderNotesUpdate.aspx' +
    data: { OrderID: OrderID, Notes: $('#txtNotes').val()},
    success:function(response) {
        /* here use can use response.responseText. For examlpe you can
           code which call the syncrone $.ajax before and used
           the return value here */
    }
})
Oleg
10x, the last answer was great for my needs, but 10x, it was very helpfull. and for the sync ajax, i need to finish the ajax call becouse i don't want the reciving page to lose any data, i am using the results from the ajax call, and i understand, every time the ajax is not just for sending data to the server but u uses that data some how, u need to use the async:false param....
Erez
@Erez: From your question it is unclear which kind of data you get from the server. If the data (`OrderNotes`) are a html fragment (and you have on your place a `<div id="myAjaxResult"/>` then you can for example use `$("#myAjaxResult").html(response.responseText)` in the body of `success` handle. In the same way you can use JSON data from the server response to fill there. So in 95% from 100 you can very easy write your program without having `async:false`. It you need block your page or page fragment I'd recommend you jQuery BlockUI Plugin: http://jquery.malsup.com/block/#demos
Oleg