reading cross domain xml in parse.cloud - parse-platform

Writing a parse.cloud code to read cross domain xml.
I have tried using jquery-ajax but am stuck with syntactical issue my code is
Parse.Cloud.define("read", function(request, response) {
var query = 'http://data.gov.in/sites/default/files/Date-Wise-Prices-all-Commodity.xml&callback=?';
$.ajax({
url: query,
type: 'GET',
dataType: 'json',
success: function(s) {
response.success("Success");
},
error: function(e)
{
response.success("Error "+e)
}
});
});
I am getting following error :
"code":141,"error":"ReferenceError: $ is not defined\n at main.js:5:20

Use Parse.Cloud.httpRequest
Parse.Cloud.httpRequest({
url: 'http://data.gov.in/sites/default/files/Date-Wise-Prices-all-Commodity.xml',
success: function(httpResponse) {
// httpResponse.data will hold the returned object
},
error: function(httpResponse) {
console.error('Request failed with response code ' + httpResponse.status);
}
});
https://www.parse.com/docs/cloud_code_guide#networking
https://parse.com/docs/js/symbols/Parse.Cloud.HTTPResponse.html

Related

Ajax fetching data.But also showing error with datatype Json.But working with datatype html

I am trying to fetch the data from php file by passing dynamic query string on ajax URL section.But while I changing datatype html to json. It is popping up error
jQuery(".nks_cc_trigger_element").click(function(){
var str = jQuery(this).attr("data-product_id");
jQuery.ajax({
url: "/ajax_product.php?q="+parseInt(str),
type: 'POST',
dataType: 'json',
success: function(result){
jQuery("#nks-content-1 > div").html(result);
console.log(result);
},
error: function(jqXHR,error, errorThrown) {
if(jqXHR.status&&jqXHR.status==400){
alert(jqXHR.responseText);
}else{
alert("Something went wrong");
alert(jqXHR.responseText);
}
}
}).done(function() {
});
>>PHP CODE
echo json_encode($data) ;
Use the data key/attribute and use the POST method, but don't pass the queries through the URL.
$(".nks_cc_trigger_element").click(function(){
var str = $(this).attr("data-product_id");
$.ajax({
url: "ajax_product.php",
type: 'POST',
dataType: 'json',
data: //json data,
success: function(result){
//success code
},
error: function(jqXHR, error, errorThrown) {
//error code
}
});
});

No Ajax response even though URL is correct?

i get error in firebug when i try to get the data from the second url, but when i try the url in comments (the first one) everything is ok,
function build()
{
$.ajax({
type: "GET",
contentType: "application/json",
crossDomain: true,
/// url: "http://localhost:9100/todo-0.1/data.json",
url:'localhost:9000/rest/admin/component?componentUri=file:/home/workspace/app/5-Integration/',
dataType: "json",
success: function (data)
{
var n=data.links.link[1]['#href'].indexOf("file");
var ContObject=new Object();
ContObject.data=new Object();
ContObject.data["title"]= decodeURI(data.links.link[1] ['#href'].substring(n));
ContObject.state="open";
ContObject.metadata=new Object();
ContObject.metadata["id"]= decodeURI(data.links.link[1]['#href'].substring(n));
var jsonText = JSON.stringify(ContObject);
var output="<div>";
output+= jsonText;
output+="</div>";
document.getElementById("placeholder").innerHTML=output;
alert("success");
},
error: function (data,status)
{
console.log("ERROR");
console.log(status);
}
});
thanks for guidance
url:'localhost:9000/rest/admin/co....
must be
url:'http://localhost:9000/rest/admin/co....
update:
enable cross domain requests: How to enable cross-domain request on the server?
debug:
success: function (data) { console.log(data); }

Ajax Call with PUT method

i am trying to make ajax call with PUT method. Below is the code, but i am getting with the error XML Parsing Error: no element found Location: moz-nullprincipal:{c847a4af-f009-4907-a103-50874fcbbe35} Line Number 1, Column 1:
$.ajax({
type: "PUT",
async: true,
url: "http://localhost:8080/karthick/update",
data: JSON.stringify(params),
contentType: "application/json",
dataType: "JSON",
processdata: true,
success: function (json) { //On Successfull service call
},
error: function (xhr) {
alert(xhr.responseText);
}
});
return false;
};
function ServiceFailed(xhr) {
alert(xhr.responseText);
if (xhr.responseText) {
var err = xhr.responseText;
if (err)
error(err);
else
error({ Message: "Unknown server error." })
}
return;
}
But this service is working Good with Rest-client jar. Also my POST method works fine in my browser. Please help me in this.
Regards
Karthick
Usually, this error comes, when making a cross browser request. Try data: JSONP and see if it helps.

Ajax Call not working - is the call right

I am trying to send an ajax call with json array
the call function is
if(objHasValue) {
alert(JSON.stringify(objArray));
alert("before ajax call");
$.ajax({
type: 'POST',
url: 'http://www.web2222.net/Test/test.php',
dataType: 'json',
data: { json: JSON.stringify(objArray) },
success: function(data) {
alert('did it-'+data);
return false;
},
error: function(data){
alert('failure'+data.json);
}
});
}
return false;
somehow it doesn't work
Do I have any mistake there?
Thanks
i am not sure if this is the problem but try something like :
data: { "json": JSON.stringify(objArray) }

asp.net mvc ajax driving me mad

how come when I send ajax request like this everything works
$(".btnDeleteSong").click(function () {
var songId = $(this).attr('name');
$.ajax({
type: 'POST',
url: "/Home/DeleteSong/",
data: { id: songId },
success: ShowMsg("Song deleted successfully"),
error: ShowMsg("There was an error therefore song could not be deleted, please try again"),
dataType: "json"
});
});
But when I add the anonymous function to the success It always showes me the error message although the song is still deleted
$(".btnDeleteSong").click(function () {
var songId = $(this).attr('name');
$.ajax({
type: 'POST',
url: "/Home/DeleteSong/",
data: { id: songId },
success: function () { ShowMsg("Song deleted successfully"); },
error: function () {
ShowMsg("There was an error therefore song could not be deleted, please try again");
},
dataType: "json"
});
});
what if i wanted few things on success of the ajax call, I need to be able to use the anonymous function and I know that's how it should be done, but what am I doing wrong?
I want the success message to show not the error one.
function ShowMsg(parameter) {
$("#msg").find("span").replaceWith(parameter);
$("#msg").css("display", "inline");
$("#msg").fadeOut(2000);
return false;
}
Make sure your action is returning Json data.
"json": Evaluates the response as JSON and returns a JavaScript object. In jQuery 1.4 the JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. (See json.org for more information on proper JSON formatting.)
http://api.jquery.com/jQuery.ajax/
Your action method should surely return Json data. I have the similar code see if that helps.
public ActionResult GetAllByFilter(Student student)
{
return Json(new { data = this.RenderPartialViewToString("PartialStudentList", _studentViewModel.GetBySearchFilter(student).ToList()) });
}
$("#btnSearch").live('click',function () {
var student = {
Name: $("#txtSearchByName").val(),
CourseID: $("#txtSearchByCourseID").val()
};
$.ajax({
url: '/StudentRep/GetAllByFilter',
type: "POST",
data: JSON.stringify(student),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(result) {
$("#dialog-modal").dialog("close");
RefreshPartialView(result.data);
}
, error: function() { alert('some error occured!!'); }
});
});
Above code is used to reload a partial view. in your case it should be straight forward.
Thanks,
Praveen

Resources