how to pass the ajax Post url with paramters to web service? - javascript

actually i have webservice running and i want to retrieve the response or output of web service (xml output) and want to show it on some web page. i am trying to give some parameters as input and sending to webservice by AJAX POST and getting some dummy response.. i have problem while sending the parameters with URL. WOULD YOU TELL ME ABOUT THE FORMAT OF AJAX POST PARAMTERS?
var params="text=text1&target=target1";
It returns some error value in response, but with the same data i am able to access with the terminal.
text=text1&target=target1 these paramters are passing as one string not different paramters
I have tried in other way also
var params='text='+text1+'&target='+target1;
but it returns nothing in response
What should I set for params value?

You can't send POST data via url query string.
You will need to either use cURL from the command line, or use a POST tool like POSTMAN to pass form data and the appropriate POST headers.
Edit: your tags suggest you're using javascript/ajax.
You can do this natively with javascript, but you'd have a significantly easier time using jQuery's $.ajax or $.post:
$.ajax({
type: "POST",
url: "http://myapp.com/someendpoint",
data: {
text: "text1",
target: "target1"
}
});

Related

Send JSON data to Flask server using JQuery .load()

I am trying to use JQuery's .load() function to send data to my Flask server and, using that, render a <div> that is loaded into the calling element. My call looks something like this:
$("#element").load(
"/get_data",
{"info": info} // this is the problem
)
However, when I try to access this data in my Flask backend, the data is of form byte-string. (Accessing with request.get_data() yields a byte-string and request.get_json() yields None).
This did not surprise me, as the same behavior occurs when I use .ajax() requests. With Ajax, I simply use data: JSON.stringify({"info":info}) and that sends a string easily read by json.loads to the Flask backend just fine. What befuddles me is when I attempt the same data packaging with .load(), the request to the server is a GET instead of a POST.
Per .load()'s documentation, "The POST method is used if data is provided as an object; otherwise, GET is assumed." I don't understand why the data being a JSON string alters the behavior of .load() this way.
I'm keen to understand this behavior, but my question is how can I send data using JQuery's .load() to a Flask backend as a POST request in form JSON, or at least readable as a json (e.g. JSON string)?
Your code should work. You have data as {"info": info}, which is an object that .load should send as a POST. Make sure you are getting a JSON mimetype object from the server. Also, make sure your Flask view accepts the POST method:
from flask import Flask, request, Response
import json
#app.route('/get_data', methods = ['GET', 'POST'])
def get_data():
payload_dict = json.loads(request.data.decode('utf-8'))
# or try payload_dict = request.json
print(payload_dict["info"])
return Response(json.dumps({"info_response": "hello"}), mimetype='application/json')
And make sure you have info defined in {"info": info}
Thought about using getJSON: https://api.jquery.com/jQuery.getJSON/ ?

How to manually submit a post request to a server?

I am looking for a way to manually submit a Post request to a server, without using the website's UI. I can see the request headers and the post parameters in Firebug when I perform the action manually (clicking the UI's "submit" button). I am hoping there is a way to reverse engineer some Javascript using these headers and parameters so that we can automate this process.
Reason: My company recently purchased some process automation software that enables us to write automation bots that access out business partner's portal site and automatically adjust our digital marketing bids. For one of our partner sites, front-end manipulation doesn't appear to work, because the Post request is submitted via AJAX.
The software does allow us to execute custom javascript within the environment, so I am trying to construct some Javascript using the headers and request parameters.
Is there a standard template into which I can plug these parameters to execute Javascript that will send the Post request to the server?
Thank you
UPDATE:
Thank you all for your help! I've made some progress but am still having difficulty implementing the solution within the software.
The following request works when I run the code in Firebug in Firefox:
$.ajax({
type: "POST",
url: "http://acp.example.com/campaigns/122828",
data: "data-string"
});
However, the software we're using might be a little out of date and I'm not sure it recognizes the AJAX syntax.
Is there a way to effectively write the same statement above, but in Javascript rather than AJAX? Then I think it would work.
You can use AJAX to post data to a server without any direct UI interaction. I will break down a simple jQuery example below:
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
});
$.ajax Is a method offered by the jQuery framework to make AJAX requests simple and cross browser compatible. As you can see I have passed in a JSON object containing various values:
type - This is the first key I have specified, in this instance you'll want this to be of the value POST as this determines the HTTP Request Method.
url - This specifies the server end point, for example: post/data/here.php would post the data to that url so that it can be picked up and handled correctly.
data - This key expects a JSON object, string or array of data to send in the POST request.
success - This key expects a function, it is called on the server's response to the request, with any relevant data passed through.
More documentation is available at: http://api.jquery.com/jquery.ajax/
If all you want to do is POST data, no JavaScript needed.
You should be able to use a browser extension for this. I have one called REST Console that is similar to what you describe. I believe REST Console is for Chrome only, but a quick Google search yielded a similar looking extension for FireFox called RESTClient.

Form post JSON data to webservice to facilitate file download with iFrame

I need to call a web service which streams data back as a file download. Initially I got everything working by using a form, hidden iFrame, and a GET:
$("<form/>").attr({
id: "downloadForm",
action: webServiceURL,
method: "GET",
target: "downloadFrame"
}).appendTo(downloadButton);
$("<iframe/>").attr({
name: "downloadFrame",
style: "visibility:hidden;display:none"
}).appendTo(downloadButton);
$('#downloadForm').submit();
$('#downloadForm').remove();
Now I have to change the web service call to a POST since the data could be big. So I have to send my parameters to the web service in JSON format like this:
{"Id1":1,"Id2":2}
I'm not sure how to do this. If I use $.ajax to do a POST with the parameters I don't know how to send the response to the iframe to start the file download.
Any help is appreciated. Thanks.

facebook - LIKE a post via ajax

I am creating a facebook wall (stream) look-a-like to put on my site.
This component will read all posts from a specific page`s wall and display them, via the graph api.
I also want the user to be able to LIKE the posts displayed on the "wall".
What I have so far is a script that uses the graph api to get the JSON list of posts and I also have a PHP file that can LIKE a post who`s ID is submitted in the post_id query string parameter, and this does work. I see the LIKE is submitted.
To call this PHP file I use jQuery ajax:
function do_likes(post_id) {
$.ajax({
type: "POST",
url:"http://www.p-art.co.il/facebook_test/action.php?post_id=" + post_id
});
Firebug doesn't show any error, but on the other hand, the LIKE is not posted.
I have been searching for several hours, but I can't find the correct way to call the PHP file, in order for the FB.api call to work.
Thank you in advance.
-Elad
With a HTTP POST, data is normally sent from form inputs with the enctype set to application/x-www-form-urlencoded format. So with an AJAX POST, we would usually send data in this format also and not as a query string parameter, which is how data is usually sent with a HTTP GET request and how you are sending data above.
if you change your code to
function do_likes(post_id) {
$.ajax({
type: "POST",
url:"http://www.p-art.co.il/facebook_test/action.php",
data : { post_id : post_id }
});
}
it should work as expected (I'm not familiar with PHP but I assume that the URL you're posting to expects data in application/x-www-form-urlencoded format). with jQuery.ajax(), if you set the data object to the key/value pairs that you want to send to the server, jQuery will take care of providing the correct enctype for you based on the HTTP request type you are using (you can override the enctype if necessary, but usually this is not required and the defaults will be what you need in the majority of cases).
Also, you may want to set a callback function to be called after the AJAX post has successfully completed. To do this add a success property to the object passed to the $.ajax() call.
It's hard to tell without seeing the source code for your action.php file but I'm guessing its not getting the users access token correctly due to it being called via AJAX.
If you can post your action.php source somewhere I should be able to help some more

Can a yui aSync Request send objects as parameters?

Is there a way that you can pass in a javascript object to a yui aSync request or does it only accept a URI?
A YUI2 AsyncRequest sends a HTTP request to a URL using the request type you specify.
You are free to append information as query parameters for a GET or POST, or to send it as POST data.
To do so you could write a simple for in loop to iterate over your object & create a query string that you can then either set as the POST data or append to your URL if sending a GET. Be sure to use encodeURIComponent on the components as you are building your string.

Categories

Resources