I have a site "A" and to test cross site posts from site "B" using jQuery I've added this in Global.asax Application_BeginRequest
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "X-Requested-With");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST");
The post from site "B" looks like this:
$.ajax({
type: 'POST',
url: rootUrl,
crossDomain: true,
data: request.toPostData(),
dataType: 'json',
success: onsuccess,
error: onerror
});
My problem now is that it lets through two requests to the controller action. One with formvalues and one without.
Of course I only want the last one, which holds the values. I hope anyone could explain and point me to a solution.
to go across domains, you need to use something like jsonp http://en.wikipedia.org/wiki/JSONP
use dataType:'jsonp' and append your url with '&callback=?'
$.ajax({
type: 'POST',
dataType:'jsonp',
url: rootUrl,
crossDomain: true,
data: request.toPostData(),
success: onsuccess,
error: onerror
});
for more reference http://www.ibm.com/developerworks/library/wa-aj-jsonp1/
Related
Studying for an exam and came across the following practice question.
You develop a web application by using jQuery. You develop the following jQuery code:
$(document).ready(function () {
$('#submit').click(function () {
$.ajax({
//INSERT CODE
data: $('#myForm').serialize(),
success: function (result) {
$('#result').text(result.message);
}
});
})
})
The web application exposes a RESTful web api that has an endpoint of product/create. You need to create a new product by using AJAX.
Which code segment should you insert at line 04?
//Option A:
type: "POST",
dataType: "xml",
contentType: "application/x-www-urlencoded; charset=UTF-8",
url: ".product/create",
//OPTION B:
type: "POST",
dataType: "json",
url: ".product/create",
Could someone explain why option B is correct?
I understand that it should be a post request since a new product is being created. Datatype could be either json or xml. Content-type is optional. Is it because result.message can only work when a json is passed in?
For the datatype:"xml", The contenttype is not valid in Option A. The valid options for XMLs are: text/xml, application/xml.
But, Option B has valid entries.
The corrected option A is below,
//Option A:
type: "POST",
dataType: "xml",
contentType: "application/xml; charset=UTF-8",
url: ".product/create",
I would like to use Meteor.call('GET') instead of $.ajax(). I have an ajax call as the following:
$.ajax({
url: url,
crossDomain:true,
type: method,
data: query,
dataType: 'json'
}).done(function(data) {
_tokens.request = {
token: data.oauth_token,
secret: data.oauth_token_secret.split('')
};
});
Have some options but I don't know how to pass to Meteor.call(). Please help!
Thank you so much
You probably mean HTTP.call or HTTP.get. Meteor.call is related to another concept.
http://docs.meteor.com/#/full/http_get
I have an asp.net MVC application.
In my global.asax:
ValueProviderFactories.Factories.Add(new JsonNetValueProviderFactory());
if I use jquery ajax
$.ajax({
url: urlPostInvitation,
type: 'POST',
dataType: 'json',
data: $.toJSON($scope.invitation),
contentType: 'application/json; charset=utf-8',
success: function (data) {
alert('Yes');
}
});
Everything works fine, but if I use AngularJS ajax
$http({
url: urlPostInvitation,
method: "POST",
data: $scope.invitation,
headers: { 'Content-Type': 'application/json; charset=utf-8' }
}).success(function (data, status, headers, config) {
alert("Yes");
});
I get an empty object in the server.
With JsonValueProviderFactory everything works fine, but not with JsonNetValueProviderFactory
If you output $scope.invitation and $.toJSON($scope.invitation) to the console do they look the same?
I answer myself, the jQuery ajax post adds the header: 'X-Requested-With': 'XMLHttpRequest'
If I add this in the AngularJs call it works
Please help me, what is wrong in this code?
$.ajax({
type: 'POST',
url: baseUrl+url,
data: {language: 'it'},
xhrFields: {
withCredentials : true
}
})
why server receives:
'language=it_IT'
Try to specify your dataType and use JSON.stringify():
$.ajax({
type: 'POST',
url: baseUrl+url,
data: JSON.stringify ({language: 'it'}),
xhrFields: {
withCredentials : true
},
contentType: "application/json",
dataType: 'json'
})
I just run through the same issue: for some reason, whenever you send language parameter using ajax it somehow automatically changes to get and all post parameters get lost. Solution: avoid using language parameter at all (or stringify yout data as #Agash Thamo suggested. That is strange for me and I would really like if somoeno could explain that a little bit better.
I have a real problem. I have a php code and a form where when the form is submitted a POST request is sent and the page reloads again and ofcourse the post is viwed in the page.But i want to work with AJAX in order page not to be refreshed.I know the basics of AJAX but i don't want to build all the project from the beggining.Is there a way in success function of AJAX to link to my php code??
$.ajax({
type: "POST",
url: "index.php",
datatype: "html",
data: dataString,
success: function(data) {
//How can i link here to start running my php code which is located in the same page.
}
});
$.ajax({
type: "POST",
url: "somescript.php",
datatype: "html",
data: dataString,
success: function(data) {
// try this
console.log(data);
// see what 'data' actually is
}
});
then check in the browser by hitting F12 to look at the console.
also are you sure you want datatype of html ? you probably want a data type of json or XML , what is the server returning to you after the ajax post?
You have to cancel the form's submission so that the ajax request will take place, otherwise it is canceled. Also use .serialize to get a name-value pair string of the form data to use in the ajax call.
Html
<form id="MyForm">
<button id="MyButtonId">Submit</button>
</form>
JS
$("#MyForm").submit(function(e){
//Prevents the form from being submitted
e.preventDefault();
$.ajax({
type: "POST",
data: $("#MyForm").serialize(),
url: "somescript.php",
datatype: "html",
data: dataString,
success: function(data) {
alert(data);
}
});
});