I want to do a serialize the data that I got from 3rd Party API. The following Picture is what I got from the API
As you can see in the photo, I have received 2 sets of the Information which indicated by the number in the []. And when I do the POST request using jQuery, the multiple Parameters Posts to my Java method. What I would like to do is to encapsulate each set ot the Information into a Group and after that serialize them in one parameter and sent it back as one object that hold all the things inside.
How can I do it by Javascrip or jQuery???
anyway, this is my code
function getMediaInfo(InkBlob){
console.log(InkBlob);
jQuery.ajax({
type: 'POST',
url: '/webapp/filepicker/importAssets',
dataType: 'json',
data: {"inkBlob": InkBlob}
}); }
All I want to do is to set only one parameter call inkBlob that contains the Information as Show in the photo above.
How about -
For Serializing js object
var str = JSON.stringify(InkBlob);
jQuery.ajax({
type: 'POST',
url: '/webapp/filepicker/importAssets',
dataType: 'json',
data: {"inkBlob": str}
});
Note
JSON.stringify is not available for older browser, use JSON2.js for older browsers.
Related
I am creating a DDBB Insert trough $.ajax:
$(document).on('click','.submitMessage', function(){
content=$('textarea').val();
img=$('#messageImg').val();
stdMsg=$('.ms_stdMsg').val();
prefix=$('.prefix').val();
phone=$('.cuadroTelefono').val();
$.ajax({
url: "../actions/newMessage.php",
type: "POST",
data:{ms_content:content, ms_img:img,ms_prefix:prefix,ms_phone:phone},
contentType: false,
cache: false,
processData:false,
success: function(data)
{
alert("Enviado");
}
});
});
And this is the way I receive code on newMessage.php:
$ms_content = $_POST['ms_content'];
$ms_img = $_POST['ms_img'];
$ms_prefix = $_POST['ms_prefix'];
$ms_phone = $_POST['ms_phone'];
Console gives an error
Notice: Undefined index: ms_content in C:...\newMessage.php on line 9
one for each variable passed (I have ommited entire URL)
As the posted information is an object, I guess I must decode it someway on PHP, but trying:
$ms_content = json_decode($_POST['ms_content']);
...has neither workd
You need to specify the data that you are sending with contentType parameter. For more references
$(document).on('click','.submitMessage', function(){
content=$('textarea').val();
img=$('#messageImg').val();
stdMsg=$('.ms_stdMsg').val();
prefix=$('.prefix').val();
phone=$('.cuadroTelefono').val();
$.ajax({
url: "../actions/newMessage.php",
type: "POST",
data:{ms_content:content, ms_img:img,ms_prefix:prefix,ms_phone:phone},
cache: false,
success: function(data)
{
alert("Enviado");
}
});
});
Please remove processData:false, contentType: false and then try.
You can use this $_REQUEST instead of $_POST in your php file.
$ms_content = $_REQUEST['ms_content'];
Instead of
$ms_content = $_POST['ms_content'];
Second Maybe due to url path
try giving full url path.
Third
Provide a contentType.
I think you have to access the $_GET parameters because jquery documentation says:
data
Type: PlainObject or String or Array
Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).
http://api.jquery.com/jquery.ajax/
So you will get your data with
$ms_img = $_GET['ms_img'];
I am trying to write a script that allows posting to a JSON file. For some reason the process is succeeding, but the JSON file isn't being written to.
Here is my jQuery code:
$(function() {
var data = {
name: 'cool',
drink: 'cool2',
};
$.ajax({
type: "POST",
url: '/api/orders',
dataType: 'json',
async: false,
data: JSON.stringify(data),
success: function() {
alert("Thanks!");
}
})
});
Here is my JSON code (/api/orders)
[
{"id":1,"name":"Ben","drink":"Americano w/ Creme"},
{"id":2,"name":"Ben2","drink":"Americano w/ Creme2"},
{"id":3,"name":"Ben3","drink":"Americano w/ Creme3"}
]
I can't figure out why Chrome is saying it is succeeding, but the code isn't posting to the JSON file.
The problem isn't with your JavaScript. The JSON.stringify just concerts the data to a JSON string and passes it to the url. Your problem will be in the Url that it is posted to /Ali/orders. That looks like a directory to me instead of an API to handle the string. I could be wrong in that as certain technologies hide that stuff. Still you need to look at where the data is going to.
I'm trying to lessen manual update work by using Yahoo Pipes and jQuery to update a table with upcoming events from a different domain. I have created this Pipe to gather the data, and it seems to work.
Now I am reading the JSON file with the following jQuery Script taken from this tutorial:
$.ajax({
type: "GET",
url: "http://pipes.yahoo.com/pipes/pipe.run?_id=57aa32bf3481c8dca0c07afcf9b9dc29&_render=json",
async: false,
beforeSend: function(x) {
if(x && x.overrideMimeType) {
x.overrideMimeType("application/j-son;charset=UTF-8");
}
},
dataType: "json",
success: function(data){
$('body').append(data.query.results.a.content);
}
});
The jQuery append failes, I guess because 'data.query.results.a.content' does not relate well to the JSON structure created by the pipe.
I've tried altering both the pipe and the append in several ways and am just short of giving up, I'd be very grateful for your Input.
Think you are parsing the json result incorrectly.
On viewing the json object here http://jsonviewer.stack.hu/
You will observe content node for each item is at value.items[0].a.content
i.e. try something like this :
$('body').append(data.value.items[0].a.content);
You will need to iterate the items array like this
$.each(data.value.items, function(index,element) {
$('body').append(element.a.content);
});
Try it on fiddle : http://jsfiddle.net/EFvJf/
Stuck on the first stage of a big project. I am trying to display the JSON value that I get from URL shown below. It shows the data when I past the URL on the a browser. But when I put the URL in variable and try to show in html it doesn't show the phrased value.(I will delete the app/key one I get the solution.) Thanks in advance.
http://jsfiddle.net/rexonms/6Adbu/
http://api.flickr.com/services/rest/?format=json&method=flickr.photosets.getPhotos&photoset_id=72157629130565949&per_page=10&page=1&api_key=ccc93a20a1bb9060fa09041fa8e19fb5&jsoncallback=?
Have you tried using the $.getJSON() or the $.ajax() method? This seems to return the data just fine:
$.getJSON(apiCall, function(data) {
console.log(data);
});
Here's a working fiddle.
Additional Information
It seems like you may benefit from a simple tutorial that explains AJAX in relation to jQuery's $.getJSON() and $.ajax() methods.
$("<span>").html(apiCall)
apiCall is a string (containing the URL). All you're doing here is setting a span to have the URL as its content. You need to actually use AJAX to load said URL.
$.ajax({
url: 'http://api.flickr.com/services/rest/',
dataType: 'jsonp',
type: 'GET',
data: {
format: 'json',
method: 'flickr.photosets.getPhotos',
photoset_id: '72157629130565949',
per_page: 10,
page: 1,
api_key: 'ccc93a20a1bb9060fa09041fa8e19fb5'
},
jsonp: 'jsoncallback',
success: function(data){
console.log(data);
}
});
Inside success, data is the JSON object returned by the API.
I would like to pass a nested JavaScript object to my ASP.NET MVC Action Method.
Here is the code(simplified):
$.getJSON('some/url',
{
index: pageIndex,
pageSize: pageSize,
filter:{one:'one',two:'two',three:'three'}
}, someCallBack(msg)
);
I'm using jQuery and implemented my plugin which lazily fetches paginated data from the server. It works all charm but now I need to pass a JavaScript Filter object with a variable number of properties-filters. On the server-side, I get an object array where the first item is a string, containing the [Object object] literal.
Obviously, my nested object (filter) is not being expanded and transformed into an object (hash) on the server-side. Is this possible at all?? I don't want to hard code my filters, since the plugin is meant to be universally applied.
Thank you very much.
You can use System.Web.Script.Serialization.JavaScriptSerializer to send/receive JSON serialized data:
JavaScriptSerializer js = new JavaScriptSerializer();
Filter f = js.Deserialize<Filter>(json_str);
More details here. To encode the JSON data to send to the server, use a JSON serialization library for JavaScript like json2.js. Then send the query like this:
var filter = {field1: 'value1', field2: 'value2'}
$.ajax({
type: "POST",
url: '/server/path',
data: { filter: JSON2.stringify(filter) },
dataType: "json",
success: function(data) {
// do something
}
});
JSON would be perfect for this. Basically, you'll want to convert your object to it's JSON representation and then send that across the wire. Once it's available on the server, you can process it however you like.
Crockford has a great article on what JSON is, how to understand the notation, and he provides a tool to convert your objects to JSON notation.
You can use the following js lib json2 library, you can then use the stringify method to ensure your json is in the correct format for the service.
var data0 = {one:'one',two:'two',three:'three'}
var json = JSON2.stringify(data0 );
$.ajax({
type: "POST",
url: someServiceUrl,
data: json,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
}
});