Call PHP rest webservice from ajax MVC asp.net? - asp.net-mvc-4

i am trying to call php rest webservice from MVC ajax but after calling it it is not going to call success i dont what i am missing here.
webservice required 2 paramaters my code
$.ajax({
type: "POST",
url: "https://test.com/custom/",
dataType: "json",
contentType: "application/json",
data: { "ReleaseVersion": "XXX", "ReleaseType":"XXX" },
success: function (data) {
alert("webservice called successfully");
}
})
it tried using json.stringfy while passing parameters but it wont work.
is anyone how to solve this.
Environment: Visual Studio 2013 and MVC 4.0.

set datatype in ajax call that is jsonp for calling cross domain webservice.
In php webservice code you have to add header before passing json.
as header content type=application/json, then and then only you will get json from php webservice in your ajax call.

Related

Crossdomain jQuery ajax request was not called and give error status "parsererror"

I am trying to call cross domain url. which has response text as below.and it is valid json response.
[{"LANG_CODE":"UK_EN","COU_ISO_CODE":"BGR"},
{"LANG_CODE":"UK_EN","COU_ISO_CODE":"HUN"},
{"LANG_CODE":"UK_EN","COU_ISO_CODE":"PRT"},
{"LANG_CODE":"UK_EN","COU_ISO_CODE":"UGA"}]
Jquery ajax code which i am using for calling cross.
$.ajax({
url: "http://someDomainName/restfulservice/Api/Countries/Get_Json",
dataType: 'jsonp',
crossDomain: true,
async: false,
success: function (data) {
alert("success >> "+data);
},
error: function (jqXHR, textStatus, errorThrown) {
alert("error : "+errorThrown + ", textStatus >> "+textStatus);
}
});
every time it goes to error block. when i inspect this service in browser then it gives response text with valid json string.but through code i am getting error "jQuery18305917718204907095_1409810925309 was not called, status: parsererror".
while this code is working for the url "http://api.geonames.org/findNearbyPlaceNameJSON?lat=47.3&lng=9&username=demo".
what could be the issue for same ?
It is sending back JSON but you have said dataType: 'jsonp'. JSON is not JSONP.
Either change the service to support JSONP or change the client to expect JSON (which might require you to find some other way to circumvent the same origin policy)

Issue with url parameter calling WCF service from JQuery

I am trying to call the WCF service from Jquery. But if I replace the url with my machine name it doesn't work. The html/jquery and WCF are both on the same web site (project). I think many people need help with this since there is no way to debug and figure out the problem. Instead of continuously changing the wcf service file, web.config file and html (ajax function) page, does anyone have a running project with this issue?
$.ajax({
type: "POST",
//work well
//url: "Service1.svc/MyFunction",
//work well
//url: "http://localhost:43969/Service1.svc/MyFunction",
//not work ???
//url: "http://PWLL-IT-056:43969/Service1.svc/MyFunction",
data: '{"Count": "' + counter + '"}',
contentType: "application/json", // content type sent to server
success: ServiceSucceeded,
error: ServiceFailed
});
Thanks for help guys,

Cross domain json POST rails 3 rest service

How can i make a cross domain POST call for a rails 3 rest service.
I have read a lot about jsonp but its only for GET method. Are there any other way to do it on rails 3. Can anybody provide me with the exact steps to make a cross domain json POST call to rails rest service.
I want to make a POST call through ajax jquery to rails server side rest service.
I tried using JSONP but it didn't work for me
Include POST as the type.
$.ajax({
type: "POST",
url: 'ruby/file/name',
data: {'foo': 'bar'},
success: function(s) {
console.log('success' + s);
},
error: function(e) {
console.log('error' + e);
}
});

Adding Custom Header to asp.net MVC 4 Web API

Is it possible to add custom headers to asp.net MVC 4 Web API?
I assume you are talking about HTTP headers. And I am not sure if you want to customize the response or the request. You can do both. Here is a blog post that shows how to add a custom header in the response using and ActionFilterAttribute. If you are using JQuery ajax to send a request to the Web API you can use the beforeSend event to add a custom header. Here is an example of using it for basic authentication.
$.ajax({
url: _url,
data: _data,
type: _type,
beforeSend: function (xhr) {
var up = username + ":" + password;
xhr.setRequestHeader("Authorization", "Basic " + up);
},
success: _successFunc,
error: _errorFunc
});

WCF Rest Service Call

I am calling a rest service in ajax.
If I add the svc file in the same project and call I am able to call the url
The below is the URL working
http://localhost:1947/GreenViewService.svc/?callback
If I add the svc in separate project layer and call in ajax I am unable to call it says 403 forbidden error and sometimes 405 error.
If I call the below url from different layer not working is it because port no. varies. should I give any access rights.
function GetDataPoints() {
alert('S');
var action = 'http://localhost:1984/GreenViewService.svc/?callback=';
alert(action);
$.ajax({
"type": "GET",
"url": action,
"cache": false,
"contentType": "application/json; charset=utf-8",
dataType: "text",
"success": function(result) {
alert(eval(result));
var json = eval(result);
//fillDropDown(json, prefix + 'DataPoint', 'NodeLabel', 'ID');
}
});
}
As you say it works when in the same project but not in a different project.
The difference is that when it is in a different project it is a cross site call.
To do a cross site call you need to use JSONP