I create a JSON object with fisrtTest.php
The JSON is correct when I open this page with WampServer ..
But I cannot do a Ajax request :/
Why ? Cross domain policy ?
$.getJSON('http://localhost/tests/fisrtTest.php',
success
);
function success(data) {
}
This is for a mobile app with phonegap
$(document).ready(function(){
$("#ibutton").click(function() {
alert("go");
$.ajax({
url: "http://localhost:8080/tests/fisrtTest.php?callback=?",
dataType: "json",
crossDomain: true,
success: function(data) {
}
});
});
});
Well, this works but firebug gets an error :
SyntaxError: invalid label
{"jack":1,"gt":2,"c":3,"d":4,"e":5}
What is your error? Can you view JSON response in the firebug console?
Below is working code
$(document).ready(function(){
$("#ibutton").click(function() {
alert("go");
$.ajax({
url: "http://localhost:8080/tests/fisrtTest.php",
dataType: "jsonp",
crossDomain: true,
jsonpCallback: "CallBackFunction"
});
});
});
function CallBackFunction(json){
//
};
Related
I am creating login with facebook application in codeigniter. So, I want to pass json data into my controller using ajax.
Here my code is like :
var data = JSON.stringify(response);
alert(data);
$.ajax(function(){
type:"post",
//contentType: 'application/json',
//dataType: 'json',
url:"<?php echo base_url().'login/insert_fb' ?>",
data:{email:email,id:id}
}).done(function(data){
alert(data);
});
Here, In my login controller I have written code like this :
function insert_fb()
{
$data = json_decode($this->input->post($response));
print_r($data);
}
Here, In data variable i got all json record.But when I call ajax function then it gives an error like Unexpected token :.
So, how can I resolve this problem?
Note : If I remove method and dataType and contentType then it gives an error like Unexpected token : at url.
You have to remove function() from your ajax:
var data = JSON.stringify(response);
alert(data);
$.ajax({
type:"post",
//contentType: 'application/json',
//dataType: 'json',
url:"<?php echo base_url().'login/insert_fb' ?>",
data:{email:email,id:id}
}).done(function(data){
alert(data);
});
I am using ajax for sending to the server that a checkbox is checked and now other inputs fields need to be required:
EDIT: ok i will change the question to be more clarified.
i have this code:
$(document).ready(function() {
$('#button_ajax').click(function() {
var request = jQuery.ajax({
type: 'get',
url: 'http://orenmizrah.git/nir1.php',
data: {'id1': $('#input_text').val()},
async: true,
success: function(){
console.log("sent");
}
});
});
and now i check that i got the info in $_GET['id1'] with php code and if $_GET['id1'] is set, echo something to the browser.
the problem is that $_GET['id1'] is set but there is no output to the browser.
what i should do?
No output is shown in the browser because nothing is done to show it.
$(document).ready(function() {
$('#button_ajax').click(function() {
var request = jQuery.ajax({
type: 'get',
url: 'http://orenmizrah.git/nir1.php',
data: {'id1': $('#input_text').val()},
async: true,
success: function(data){
console.log(data); // outputs the returned data to the console
}
});
});
I couldn't access the webservice call from cross domain. Please advice. I have pasted my source here.
PHP: webservice response
$data['response'] = 2;
header('Content-type: application/json');
echo json_encode($data);
jQuery Ajax Call:
$.ajax({
type: 'GET',
url: cross-domain-url,
data:{ param1:'123', param2:'123' },
dataType:'jsonp',
crossDomain: 'true',
async: true,
success:function (data) {
alert('success');
},
error:function(){
alert('error');
}
});
Cross Domain URL response:
{"ResultCode":2}
Always I am getting Error only. I don't know why. I can see the following message in Firefox inspect area.
SyntaxError: missing ; before statement
{"ResultCode":2}
Please help me.
Solution:
Modify the line like,
echo 'someCallBackString('.json_encode($data).');';
instead of echo json_encode($data);
Created the function someCallBackString and proceeded my implementations.
You are telling jQuery to make a JSON-P request (dataType:'jsonp'), but your server is returning JSON.
Consequently the browser is trying to execute a JSON text as if it was a JavaScript program (and it can't because it isn't).
Either:
Remove the dataType property from the object so that jQuery will use the HTTP Content-Type to determine that it is JSON and add access control headers to permit your site to access the data on the other domain or
Return JSON-P instead of JSON
Ok the problem is in the JSONP data, when you send a request the JSON response would send a response as
callbackFunctionName(jsonData)
You do not have anything as such so is the issue you need to format the code as below
$(document).ready(function() {
$.ajax({
type: 'GET',
url: 'your cross domain file',
dataType:'jsonp',
crossDomain: 'true',
jsonpCallback: 'MyCallback',
async: true,
success:function (data) {
console.log(data);
},
error:function(data){
console.log('error');
}
});
function MyCallback(data) {
}
});
Now in the response the data needs to be formatted as
$data['response'] = 2;
header('Content-type: application/json');
$json_data = json_encode($data);
echo "MyCallback" . '(' . $json_data . ');';
You can now check your console and see the data is coming through
You can check more on this here http://remysharp.com/2007/10/08/what-is-jsonp/
change your ajax to this code:
Solution
$.ajax({
type: 'GET',
url: cross-domain-url,
data:{ param1:'123', param2:'123' },
dataType:'JSON',
success:function (data) {
alert('success');
},
error:function(){
alert('error');
}
});
Hope it will work....
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..
I need to run some PHP code from a file when I click a button from jQuery. This is the code I have:
$(document).ready(function(){
$("#btnSubmit").button().click(function(){
alert("button 1");
$.ajax({
url: 'releaseBackEnd.php',
type: 'POST',
async: false,
data: {},
dataType: 'xml',
error: function(){
alert('Error');
},
success: function(data){
//check error
alert("success!");
}
});
});
});
It shows the alert of "error". I have the releaseBackEnd.php file in the same directory of the javascript file.
check the response in firebug console .. if u don't have firebug add it to your firefox using this link
https://addons.mozilla.org/en-US/firefox/addon/1843/
Change
$("#btnSubmit").button().click(
to
$("#btnSubmit").click(
and check whether the php page is sending the response or not.
Try getting the error in the error callback function using the XMLHttpRequest.
error(XMLHttpRequest, textStatus) {
}
Try this code...
$(document).ready(function(){
$("#btnSubmit").bind('click',function() {
alert("button 1");
$.ajax({
url: 'releaseBackEnd.php',
type: 'POST',
async: false,
data: {},
dataType: 'xml',
error: function(){
alert('Error');
},
success: function(data){
//check error
alert("success!");
}
});
});
});