Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery ajax send string as POST

Tags:

jquery

ajax

I am developping a wordpress plugin, want to send string as post ajax parameters, but the string breaks with '&' code is

var data = "http://localhost/wordpress/?page_id=1&setval=RFZ83WSXa816yc6DNcgfHlgIkztR7KEC6JHRHCCcwfw|~HBZW9j3B59f8rCXO_QLY-gG2MDAcKo6fKG2AnbYnMns|~KA1KUT_SuU9W2UDTnngTsbJiptTvGWZAAzTfN5BCHak|~1";

$.ajax({
           data: data
           type: "POST",
           url: '<?php echo plugins_url().'/page-loader/createMetaDetails.php'; ?>',
           data :data,
           success: function(msg){
             alert('wow'+msg);
           }
         });

it is not working only passing till 'http://localhost/wordpress/?page_id=1', why?

like image 275
Vidya L Avatar asked Nov 19 '12 06:11

Vidya L


People also ask

How do I send a post request in Ajax?

Send Http POST request using ajax()In the options parameter, we have specified a type option as a POST, so ajax() method will send http POST request. Also, we have specified data option as a JSON object containing data which will be submitted to the server.

How can we send data to server using Ajax?

The jQuery. post( url, [data], [callback], [type] ) method loads a page from the server using a POST HTTP request. data − This optional parameter represents key/value pairs or the return value of the . serialize() function that will be sent to the server.

What is dataType in Ajax call?

dataType The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response. "text": A plain text string. So you want contentType to be application/json and dataType to be text : $.


2 Answers

You need to put data in key value pair array to pass jquery ajax function.

change

var data = 'http://localhost/wordpress/?page_id=1&setval=RFZ83WSXa816yc6DNcgfHlgIkztR7KEC6JHRHCCcwfw|~HBZW9j3B59f8rCXO_QLY-gG2MDAcKo6fKG2AnbYnMns|~KA1KUT_SuU9W2UDTnngTsbJiptTvGWZAAzTfN5BCHak|~1'

To

var data = { yoururl:'http://localhost/wordpress/?page_id=1&setval=RFZ83WSXa816yc6DNcgfHlgIkztR7KEC6JHRHCCcwfw|~HBZW9j3B59f8rCXO_QLY-gG2MDAcKo6fKG2AnbYnMns|~KA1KUT_SuU9W2UDTnngTsbJiptTvGWZAAzTfN5BCHak|~1'}
like image 67
Adil Avatar answered Oct 02 '22 09:10

Adil


The data property should be a Javascript object in key:value format; the keys will be the form field names.

like image 30
Barmar Avatar answered Oct 02 '22 10:10

Barmar