How to get ajax querystring from JavaScript given data object? - javascript

When you make an AJAX request using jQuery, you can specify the data property that will be passed to the server. If the data contains an array of objects it passes it to the server in a format something like this:
MyArray[0][MyPropertyA]=123&MyArray[1][MyPropertyA]=456
Is there a way to get the query string it will generate for the AJAX request without actually doing the AJAX request?

jQuery param is your friend.
From the jQuery Param page:
Create a serialized representation of an array or object, suitable for use in a URL query string or Ajax request.

Related

Why is my $_POST empty but file_get_contents('php://input') is not?

When I POST data to my server using a regular old form submit I can pull that data from the $_POST variable, but when POSTing JSON data via AJAX I need to access it via file_get_contents('php://input'). Why is that? In both cases I am using the POST method, are there some explicit headers I should be setting on my AJAX call? I have only ever come up against this problem on the current development server and have never had to use file_get_contents('php://input') before. Is there a server setting somewhere? Can I change this behaviour with a .htaccess?
Add this to the top of your .php file:
$_POST = json_decode(file_get_contents("php://input"), true);
so that the contents will be property decoded and available. After that, you can access individual keys as usual.
So as far as I have been able to find out, this has something to do with the way the data is received by the server - PHP can't natively parse JSON. Sending plain old JSON objects from Javascript to PHP will result in PHP not knowing what to do with the datatype, and thus it won't be able to prepopulate the appropriate global variables.
In order to get around it I added a check to my ajax wrapper function that intercepts the JSON, encodes is as a FormData object and shoots that off to the server instead.

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

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"
}
});

using backbone to put data into rest server

I have a question.
If i have a model named Input. It contains model Invoice and Collection InvoiceDetailCollection.
I modified the backbone.js specifically create:"POST" into create:"PUT" inorder to allow PUT since my service doesn't use POST.
If i were to use Input.save() What should my server expect as a request? I mean already have set a service method to expect string since if i expect string i can't use the input.save();
What should be the right parameter i should expect on my server side if i were to use the Input.save() method of backbone.js
By default, Backbone sends application/json data and the server should expects JSON data. You'll want to decode it. I use PHP and middleware that automatically json_decode($data) and turns it into an associative array for me to manipulate.
If this isn't possible, (can't take JSON data) I think what you want is to use emulateJSON It will serialize your data and send it as application/x-www-form-urlencoded like an HTML form.
http://documentcloud.github.com/backbone/#Sync-emulateJSON

How to create a JQuery or Javascript Array from HTML Formatted HTTP Response?

I'm using a service called TrueFX that provides currency information based on HTTP request/response. I need to send the initial request to the base URL with username, and other parameters. The service responds with a session ID. A subsequent HTTP request is sent with the session ID and the instructions for output type (CSV or HTML). The second response provides an HTML table with the requested data.
The final destination for this data will be a chart via the JQPlot plugin for JQuery. JQPlot will accept an array instead of individual values to be plotted.
Questions:
Is it best to create the array of data using JQuery, plain ole Javascript or PHP?
Depending on response to above, how do I define the request to TrueFX, the TrueFX response containing currency data in HTML format? Is this just done through the $.ajax JQuery method?
How do I create and save the array for reference and use in my call to JQPlot?
Thanks so much for your advice, help and guidance.
First off I would recommend you work with the CSV format as it will be much easier to parse into an array of arrays for use in your chart (also data size will be much smaller). There are multiple CSV plugins for jquery that make this parsing pretty easy. Here is one I found real quick: CSV Plugin
You can use jquery's $.ajax to get the data from the remote service (using JSONP) and the CSV plugin to parse the returned data. Then you feed the resulting array of arrays into your chart.

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