decode a data URL in php - javascript

I need to decode a data url in PHP .This data url is obtained via ajax.
I have used file reader to get the encoded data URL of an image.This data URL is passed to PHP via ajax:
$.ajax({
url: app,
async: false,
type:"POST",
data : "file="+strGlobalImageData,
dataType: "jsonp",
contentType: 'application/x-www-form-urlencoded',
processData:false,
jsonp: "jsoncallback",
success: function(html){
alert("Thank you. We will be in touch with you");
},
error: function(){
alert("Thank you. We will be in touch with you");
}
});
How will I do this?

$image =$_POST['file'] ;
Was this something you were looking for? Let me know

Related

How to access this data in Javascript?

I have this encode in JSON format as returned data:
{
"error": {
"msg":"Concurrent verifications to the same number are not allowed",
"code":10
}
}
and I want to access msg so I wrote the javascript as:
$("#buttonPhoneSubmit").click(function(e) {
$("#verifyForm").show();
e.preventDefault();
$.ajax({
type: 'post',
url: './process.php',
data: $('form').serialize(),
datatype:'json',
success: function (data) {
$('#error_message').html(data.error.msg);
}
});
but it said the data is undefined. Can someone tell me what's wrong with the code?
Thanks!
As Roy said, you have datatype: 'json' instead of dataType: 'json'. So I suspect jQuery isn't parsing the JSON for you.
While you could change it to dataType: 'json' instead, the better approach is to update the PHP file to send the Content-Type: application/json header with the response:
// In the PHP, prior to sending the body of the response
header('Content-Type: application/json');
...and remove datatype: 'json' from your ajax call. jQuery will see the content type and parse it for you, at which point your code should work (assuming the page return returns the JSON you've quoted).

Javascript POST to API

So I am a bit lost and hoping you can help me out. I am writing an app in simple PHP/HTML/Javascript app.
My Goal: To POST json data to an API.
How can I go about this? I just can't find any good examples to show me the best way to handle this.
In my request I need to send Basic Authorization as well as the json values.
This is what I have right now
$.ajax({
type: "POST",
url: "host.com/api/comments",
dataType: 'json',
async: false,
headers: {
"Authorization": "Basic xxxxxxxxxxxxxxxxxxxxxxx"
},
data: '{"value1":"2.0", "value2":"setPowerState", "value3":{"state":0}}',
success: function (){
alert('Comment Submitted');
}
});
I can't get the above code to work. Im using a button to call a function that will start the ajax call but nothing is happening.
Any help be be amazing! Thank You.
Use
contentType:"application/json"
You need to use JSON.stringify method to convert it to JSON format when you send it,
And the model binding will bind the json data to your class object.
The below code will work fine (tested)
$(function () {
var customer = {contact_name :"Scott",company_name:"HP"};
$.ajax({
type: "POST",
data :JSON.stringify(customer),
url: "api/Customer",
contentType: "application/json"
});
});
If you're writing the API in PHP, and it uses $_POST to get the parameters, you shouldn't send JSON. PHP only knows how to decode multipart/form-data and application/x-www-form-urlencode. If you pass an object to $.ajax, jQuery will use the urlencode format.
Just take the quotes off the object that you're passing to the data: option.
$.ajax({
type: "POST",
url: "host.com/api/comments",
dataType: 'json',
async: false,
headers: {
"Authorization": "Basic xxxxxxxxxxxxxxxxxxxxxxx"
},
data: {"value1":"2.0", "value2":"setPowerState", "value3":{"state":0}},
success: function (){
alert('Comment Submitted');
}
});
You also shouldn't use async: false, it is deprecated. Learn to write proper async code.
Nobody seems to have addressed one issue - the URL
If the page this is requested from is http://yourhost.com/path/file.html the request will be sent as http://yourhost.com/path/host.com/api/comments
As you have host.com in the URL, I assumed the request is to a different domain?
use one of
http://host.com/api/comments
https://host.com/api/comments
//host.com/api/comments
will only work if your page is loaded http and not https
will work only if the remote API supports https
will only always work properly if the remote API supports both http and https
The other issue is regarding the format of the sent data
The default content-type for $.ajax POST is application/x-www-form-urlencoded; charset=UTF-8
So, sending a POST request with various combinations of contentType and data shows the following
Firstly, without setting contentType
data: '{"value1":"2.0", "value2":"setPowerState", "value3":{"state":0}}'
request is sent as formData '{"value1":"2.0", "value2":"setPowerState", "value3":{"state":0}}'
data: {"value1":"2.0", "value2":"setPowerState", "value3":{"state":0}},
request is sent as formdata, the following values:
value1: 2.0
value2: setPowerState
value3[state]: 0
looks better, because there's actually multiple values, not just a string
Now, let's set contentType
contentType: 'json', data: {"value1":"2.0", "value2":"setPowerState", "value3":{"state":0}},
firefox does not tell me the format of the following string: 'value1=2.0&value2=setPowerState&value3%5Bstate%5D=0' - looks useless
And finally
contentType: 'json', data: '{"value1":"2.0", "value2":"setPowerState", "value3":{"state":0}}',
sends the following JSON: '{"value1":"2.0", "value2":"setPowerState", "value3":{"state":0}}'
So, finally, if the API requires JSON request data, and it's actually on a domain called "host.com"
$.ajax({
type: "POST",
url: "//host.com/api/comments",
dataType: 'json',
contentType: 'json', data: '{"value1":"2.0", "value2":"setPowerState", "value3":{"state":0}}',
});

Get JSON response using jQuery.ajax request to an appengine URL?

I tried to get response data using ajax from the below url
http://recipesid.appspot.com/api.user?method=user.query&email=dam.le#anttek.com
But it will not run anyway.
Help me to solve the issue
$.ajax({
type: "GET", //rest Type
dataType: 'jsonp', //mispelled
url: "http://recipesid.appspot.com/api.user?method=user.query&email=dam.le#anttek.com",
async: false,
contentType: "application/json; charset=UTF-8",
success: function (msg) {
alert(msg);
},
error:
function(data){
alert("error");
} });
Thanks in advance !!
Thanks for all answers. but i got a solution.
I must allow "Access-Control-Allow-Origin" in my google app engine.

JSONP request not responding

I have a problem with my code and i can't get it to work.
The request should go to my main domain, grab the version number and post it on the application but this does not happen.
Code:
$.ajax({
url: 'http://maindomain.com/dbstruk/version',
dataType: 'jsonp',
jsonp: 'callback',
jsonpCallback: 'jsonpCallbackSPP',
success: function(data){
$('#latest-version').text(data.version/100);
}
});
The location: maindomain.com/dbstruk/version contains only one line:
{"version":"105"}
Am i overseeing something?
Thank you.

javascript soap error calling asp.net web service

I have a working ASP.Net test web service, but I keep getting 500 errors as:
"System.InvalidOperationException: Request format is invalid: text/xml.
at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
"
when I call it with javascript.
It is a simple web service that takes a single parameter as a string and returns it to the client. Please help!
link to code here
For those of you who this might help, the issue was setting the SOAPAction in teh header correctly:
$.ajax({
type: "post",
url: target,
contentType: "text/xml",
data: soapBody,
dataType: "xml",
processData: false,
beforeSend: function( xhr ){
xhr.setRequestHeader(
"SOAPAction",
"http://blahblah.com/Services/MethodName"
);
},
....
Make sure that your mess variable doesn't contain GET-style query string like '?a=1&b=2'. You need to send it in POST format, for example in JSON. Try to change contentType to contentType: "application/json; charset=utf-8"
$.ajax({
url: service,
type: "POST",
dataType: "xml",
data: '{key: value}',
complete: endTest,
error: processError,
contentType: "application/json; charset=utf-8",
});

Categories