Passing Parameters in Ajax to JSON web service - php

I am using AJAX to request some information from a PHP built web service however the parameters I am passing doesn't seem to go to the web service my code is below:
$(document).on( "pageinit", "#player", function( e ) {
var passedId = (passDataObject.selectedHref != null ? passDataObject.selectedHref : window.location.href).replace( /.*id=/, "" );
alert(passedId); // test passedId has the correct value within it
var surl = "a working url";
$.ajax({
type: "GET",
url: surl,
data: "&Track="+passedId,
dataType: "jsonp",
cache : false,
jsonp : "onJSONPLoad",
jsonpCallback: "trackcallback",
crossDomain: "true",
success: function(response) {
alert('tracks function');
},
error: function (xhr, status) {
alert('Unknown error ' + status);
}
});
});
//callback function for player page
function trackcallback(rtndata)
{
alert(rtndata.track_name); // show up as undefined
}
The passedId has the correct value within it and the URL is fine however the web service does not produce a result even though the SQL statement is fine. I am assuming the issue is within this line within my php web service $id = $_REQUEST['Track']; as this gets the value from the JavaScript to execute the SQL.
Can anyone solve this issue?

See this code below, there is no "&" before the parameter name:
$.ajax({
url: homeUrl + "/Getsomething",
data: "parameterhere=" +$(element).attr('attrHere'),
type: 'GET',
contentType: "application/json",
dataType: "json",
cache: false,
success: function (result) {
//do stuff
},
error: function (xhr, ajaxOptions, thrownError) {
//error handling
}
});
What happens if you do you ajax get like the code above?

Related

CORS Ajax POST variables empty in live server but work in local server

After days (and a bit of spamming here and there), I did get CORS to work for my CodeIgniter applications. However, POST variables are empty in the PHP side in live server. the whole $_POST array is empty for some reason. Here is my ajax code:
var postForm = {'e':'sammy'}; //data to process
$.ajax({
type: "POST",
url: "http://www.abc.ca/types/add",
data: postForm,
dataType : "json",
cache: "false",
contentType: "application/json",
success: function (result) {
//result here is blank
alert(result);
},
fail: function (result){
alert(result);
}
});
and the called php function (without the whole controller class):
function add() {
//Add new a biz type
file_put_contents('trial.txt',implode(" | ",$_POST));
echo $this->input->post('e');
}
Am I missing something? the response from server are ok. but still the file created contains no data and echo prints empty. Any tips please? I have already tried method:'POST' instead of type:'POST'. Get variables pass away successfully.
Is this CI issue or Ajax because it is constant across ff,chrome and Opera
Try This Ajax :
$.ajax({
type: "POST",
url: "http://www.abc.ca/types/add",
data: postForm,
cache: "false",
success: function (result) {
//result here is blank
alert(result);
},
fail: function (result){
alert(result);
}
});

AJAX request returning error

I have an AJAX request that is throwing an error.
It's part of a larger system that I've inherited, so I can't post the files in their entirety, but I've tried to isolate the relevant code:
$.ajax(
{
url: ajax_url, // Valid URL
data: jsonData, // Valid JSON
dataType: 'json',
contentType: 'application/json',
type: POST,
async:true,
success: function(data)
{
console.log(data);
parsedData = JSON.parse(data);
console.log(parsedData);
// Here I am trying to log successful output
},
error: function(jsonData)
{
console.log("error");
console.log(jsonData);
}
});
In the PHP, values are added to an array and returned:
$data['status']=200;
$data['new_id']=$insert_id;
return json_encode($data);
All I am logging at the moment is 'error'. Apologies again for incomplete code, I have tried to supply the code where I think the error is caused.

how to add header while posting a url and raw data in php

i have to pass header information while posting a url and want to pass raw data in the url can any on help me on this
i have tried
function test(){
alert("test");
$.ajax({
type:"POST",
beforeSend: function (request)
{
request.setRequestHeader("X-APIKEY", "y5q9q1at8u-1uf4bao2yq-bsjdj3gh1g-u9ymh1t2f8-tt85pn4r50");
},
url: "https://backoffice.hostcontrol.com/api/v1/domain-is",
data: {"domain": "mahrosh.com"},
processData: false,
success: function(msg) {
alert(msg);
$("#results").append("The result =" + StringifyPretty(msg));
}
});
}
its not returning me results
You can add header variables as per given in below described code
function test(){
alert("test");
$.ajax({
type:"POST",
headers: { 'X-APIKEY': 'y5q9q1at8u-1uf4bao2yq-bsjdj3gh1g-u9ymh1t2f8-tt85pn4r50'},
url: "https://backoffice.hostcontrol.com/api/v1/domain-is",
data: {"domain": "mahrosh.com"},
processData: false,
success: function(data) {
alert(JSON.stringify(data));
},
error: function(data){
alert(JSON.stringify(data));
}
});
}
If server gives error in response than you have idea about it so i added the error section as well ...

Form query string using ajax and php

I am calling a ajax function on button click.
$.ajax({
type: 'GET',
url: "javascript.php?orderid=CF450AA4",
//data: "orderid=CF450AA4",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg, status) {
alert("successful");
console.log(msg);
},
error: function (msg, status) {
console.log("failure");
console.log(msg);
alert("failure");
}
});
}
I have the javascipt.php file where the code is written for server side and is working fine.
I want to recode it. I want that when someone clicks on button the url change to index.php?orderid=CF450AA4 and result is delivered in the variable. How to do this
i did it by doing..
location.hash = 'url';
Hope will work for you..

jQuery ajax not passing data JSON value

I am having some issues with the jQuery Ajax function and PHP.
I am checking the existance of the nav key in the $_REQUEST variable in PHP with code such as this:
if ($_REQUEST['nav']) {
// do something
} else {
echo 'Please specify NAV.';
}
However the above expression never evaluates as nav is never passed to it and always outputs 'Please specify NAV.'
console.log('paramList: ' + paramList);
$.ajax({
type: 'POST',
url: '/admin/nav_builder/edit.php?act=save&nav_id=<?php echo $nav_id; ?>',
data: {'nav':paramList},
dataType: 'json',
error: function(xhr, err) {
loadLayout();
hideLoader();
hideLoaderPalette();
},
success: function(data){
$('.errorMsg').html(data.html);
hideLoader();
hideLoaderPalette();
}
});
Using the Firefox Firebug plugin I can see that paramList does indeed hold a value, this is:
paramList:
{"section0":{"elem0":{"nav_palette":"text","nav_name":"fdgfdgdfg","nav_url":""},"elem1":{"nav_palette":"category","c_id":"226"}}}
I can't see for the life of my why nav is not being passed to the URL provided to the ajax function.
Try to print first if the "nav" parameter if it has a value.
echo $_REQUEST['nav'];
The other one is that you have used two method in one request.
Just try the following.
$.ajax({
type: 'POST',
url: '/admin/nav_builder/edit.php',
data: {act:'save', nav_id:'<?php echo $nav_id; ?>', nav:paramList},
dataType: 'json',
error: function(xhr, err) {
loadLayout();
hideLoader();
hideLoaderPalette();
},
success: function(data){
$('.errorMsg').html(data.html);
hideLoader();
hideLoaderPalette();
}
});
Try $_POST instead. As you're posting the nav contents.

Categories