doPost not getting called by embedded Jetty, when using Context collection - servlets

I am using Jetty 6 in embedded mode. I have a number of servlets in a ContextHandlerCollection. Beyond this problem, the servlets work fine on their different URLs.
ContextHandlerCollection contexts = new ContextHandlerCollection();
server.setHandler(contexts);
HandlerContainer mainhandler = contexts;
Context qxrpc = new Context(contexts,"/api",Context.SESSIONS);
ServletHolder rpcServHolder = new ServletHolder(new FrzRpcServlet());
rpcServHolder.setInitParameter("referrerCheck", "public");
// allows cross-domain calls
qxrpc.addServlet( rpcServHolder, "*.qxrpc");
Context statscontext =new Context(contexts,"/stats",Context.SESSIONS);
ServletHolder statsHolder = new ServletHolder(new FrzStatsServlet());
statsHolder.setInitParameter("restrictToLocalhost", "false");
// allows cross-domain calls
statscontext.addServlet(statsHolder, "/*");
Context hellocontext = new Context(contexts,"/hello", Context.SESSIONS);
hellocontext.addServlet(new ServletHolder(new HelloServlet("HELLO TEST: ")),
"/*");
Context zdbcontext = new Context(contexts,"/zdb", Context.ALL);
ServletHolder zdbHolder = new ServletHolder(new FrzZdbServlet());
statsHolder.setInitParameter("restrictToLocalhost", "false");
// allows cross-domain calls
zdbcontext.addServlet(zdbHolder, "/*");
Context root = new Context(mainhandler,"/",Context.SESSIONS);
root.setResourceBase(docroot);
root.addServlet(DefaultServlet.class, "/");
I know the POST request is coming across to my server. Here is some ngrep output:
T 127.0.0.1:51634 -> 127.0.0.1:8080 [AP]
GET /zdb/test.123:1.1.local1.stringtest HTTP/1.1..Host: 127.0.0.1:8080..Connection: keep-alive..Referer: http://127.0.0.1:8888/GWT_ZDB_editor.html?gwt.codesvr=127.0.0.1:9997..Origin: http://127.0.0.1:8888..User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.71 Safari/534.24..Content-Type: text/plain; charset=utf-8..Accept: */*..Accept-Encoding: gzip,deflate,sdch..Accept-Language: en-US,en;q=0.8..Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3....
##
T 127.0.0.1:8080 -> 127.0.0.1:51634 [AP]
HTTP/1.1 200 OK..Access-Control-Allow-Origin: *..Content-Type: application/json; charset=ISO-8859-1..Content-Length: 124..Server: Jetty(6.1.15)....
##
T 127.0.0.1:8080 -> 127.0.0.1:51634 [AP]
{ "r":0,"D":"test.123:1.1.local1.stringtest","m":"OK","t":0,"p": {"ztype": "STRING", "dat" : { "cp":0, "v": "test12131" }}}
##
Unsuccessful POST - reports 200 OK - but never gets to servlet
T 127.0.0.1:51634 -> 127.0.0.1:8080 [AP]
OPTIONS /zdb/test.123:1.1.local1.stringtest/put HTTP/1.1..Host: 127.0.0.1:8080..Connection: keep-alive..Referer: http://127.0.0.1:8888/GWT_ZDB_editor.html?gwt.codesvr=127.0.0.1:9997..Access-Control-Request-Method: POST..Origin: http://127.0.0.1:8888..User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.71 Safari/534.24..Access-Control-Request-Headers: content-type..Accept: */*..Accept-Encoding: gzip,deflate,sdch..Accept-Language: en-US,en;q=0.8..Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3....
#
T 127.0.0.1:8080 -> 127.0.0.1:51634 [AP]
HTTP/1.1 200 OK..Allow: GET, HEAD, POST, TRACE, OPTIONS..Content-Length: 0..Server: Jetty(6.1.15)...
.
What I can't figure out is why the doPost() is not getting called, while the doGet() is. The servlet in question is the FrzZdbServlet.
Found a number of threads on Google, but the Jetty folks only point back to examples, which in turn only implement do doGet() for the Context examples. As in here
Also, I am posting from GWT code, and I am using content-type application/json. Could this be the issue? Any pointers would be appreciated.

My Context apparently did not accept POSTs with content-type: application/json. Removing this on my client code fixed it. If anyone else has input appreciate it.

Related

QNetworkRequest with headers

I am using Qt to interact with a GraphQL host. I am using QNetworkRequest, and the login process is done well, gives me a token in JSON format. Since then, GraphQL expects the token to be in the HTTP header:
{
"Authorization": "Bearer <token>"
}
Testing the server, I wrote a small Python code, which works well:
headers = {'Authorization': "Bearer {0}".format(token)}
url = "http://example.com:8000/graphql"
params = {'query': '{fieldQueries{fields(userId:"xxx"){fieldName fieldID}}}'}
result = requests.post(url, params=params, headers=headers)
print(result.json())
The below code is supposed to do the same operation in Qt:
QUrl url = QUrl("http://example.com:8000/graphql");
QNetworkAccessManager * mgr = new QNetworkAccessManager(this);
QNetworkRequest request(url);
QString query = QString("{fieldQueries{fields(userId:\"%1\"){fieldName fieldID}}}").arg(userId);
QUrlQuery params;
params.addQueryItem("query", query);
connect(mgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(onQueryFinish(QNetworkReply*)));
connect(mgr, SIGNAL(finished(QNetworkReply*)), mgr, SLOT(deleteLater()));
auto header = QString("Bearer %1").arg(token);
request.setRawHeader(QByteArray("Authorization"), header.toUtf8());
mgr->post(request, params.query().toUtf8());
However, server gives back an internal server error (500).
As soon as I comment out the request.setRawHeader, server gives back You are not authorized to run this query.\nNot authenticated with no error.
How to make Qt to send this header correctly?
I don't know if it helps, but I checked the packets using WireShark. The Python generated packet for this request is a single packet (around 750 bytes), though Qt request is broken into two packets which the length of the first is 600 byte.
The working package:
...POST /graphql?query=%7BfieldQueries%7Bfields%28userId%3A%22xxx%22%29%7BfieldName+fieldID%7D%7D%7D
HTTP/1.1..Host: xxx:8000..
User-Agent: python-requests/2.21.0..
Accept-Encoding: gzip, deflate..
Accept: */*..
Connection: keep-alive..
Content-Type: application/json..
Authorization: Bearer <688 bytes token>..Content-Length: 0....
Qt generated packages:
....POST /graphql HTTP/1.1..Host: xxx:8000..
Authorization: Bearer <364 bytes token>..
Content-Type: application/json..
Content-Length: 113..
Connection: Keep-Alive..
Accept-Encoding: gzip, deflate..
Accept-Language: en-US,*..
User-Agent: Mozilla/5.0....
and
.e..query=%7BfieldQueries%7Bfields(useId:%22xxx%22)%7BfieldName fieldID%7D%7D%7D
I have checked other given solutions for the header, such as
Correct format for HTTP POST using QNetworkRequest,
Sending HTTP Header Info with Qt QNetworkAccessManagerand read the
QNetworkRequest Class documentation.
Graphql accepts both POST an GET requests. Therefore, instead of post, I used GET. It sends the query as a part of the URL, not the header, which from the captured packets I realized they are sent as the body.
The solution is as follows:
QNetworkAccessManager * mgr = new QNetworkAccessManager(this);
QString query = QString("{fieldQueries{fields(userId:\"%1\"){fieldName fieldID}}}").arg(userId);
QUrl url = QUrl(QString("http://example.com:8000/graphql?query=%1").arg(query));
QNetworkRequest request(url);
connect(mgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(onQueryFinish(QNetworkReply*)));
connect(mgr, SIGNAL(finished(QNetworkReply*)), mgr, SLOT(deleteLater()));
auto header = QString("Bearer %1").arg(token);
request.setRawHeader(QByteArray("Authorization"), header.toUtf8());
mgr->get(request);

Streaming mp4 requests via http with range header in grails

I'm on an old grails 2.5.1 app and I noticed mp4 video files served from the server don't play in Safari. I looked up the issue on SO and got some hints that it has to do with the range header. But I suspect the way I'm handling the range header isn't quite right.
So far, what I've found is Mac OS Safari 11.0 (11604.1.38.1.7) (I don't care about ios Safari right now) sends two GET requests. Firstly, it sends one with:
host: localhost:8080
accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Safari/604.1.38
accept-language: en-us
accept-encoding: gzip, deflate
x-request-time: t=****
x-forwarded-for: *.*.*.*
x-forwarded-host: *.com
x-forwarded-server: *.com
connection: Keep-Alive
cookie: ...TOO BIG TO SHOW HERE
<- "GET /.../videos/lol.mp4 HTTP/1.1" 200 186ms
Subsequently, it sends second GET request:
host: localhost:8080
language: en-us
playback-session-id: 03F1B4E6-F97E-****
bytes=0-1
accept: */*
user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Safari/604.1.38
https://.../videos/lol.mp4
encoding: identity
request-time: t=****
forwarded-for: *.*.*.*
forwarded-host: *.com
forwarded-server: *.com
connection: Keep-Alive
cookie: ...TOO BIG TO SHOW HERE
<- "GET /uiv2/videos/lol.mp4 HTTP/1.1" 206 149ms
Debugging this is hard because Safari web inspector doesn't show you much. In fact, it doesn't even show you all the headers it sends so I had to get this from the back end.
As can be seen, the difference between request 1 and 2 is the 2nd has playback-session-id and range.
The hard part is finding out how to please Safari in how range and playback-session-id are handled.
I've made a controller to return the range of bytes requested, if they're requested. But still no luck.
import grails.compiler.GrailsTypeChecked
import grails.plugin.springsecurity.annotation.Secured
import asset.pipeline.grails.AssetResourceLocator
import grails.util.BuildSettings
import org.codehaus.groovy.grails.commons.GrailsApplication
import org.springframework.core.io.Resource
class VideoController {
GrailsApplication grailsApplication
AssetResourceLocator assetResourceLocator
public index() {
Resource mp4Resource = assetResourceLocator.findAssetForURI('/../lol.mp4');
response.addHeader("Content-type", "video/mp4")
response.addHeader( 'Accept-Ranges', 'bytes')
String range = request.getHeader('range')
if(range) {
String[] rangeKeyValue = range.split('=')
String[] rangeEnds = rangeKeyValue[1].split('-')
if(rangeEnds.length > 1) {
int startByte = Integer.parseInt(rangeEnds[0])
int endByte = Integer.parseInt(rangeEnds[1])
int contentLength = (endByte - startByte) + 1
byte[] inputBytes = new byte[contentLength]
mp4Resource.inputStream.read(inputBytes, startByte, contentLength)
response.status = 206
response.addHeader( 'Content-Length', "${contentLength}")
response.outputStream << inputBytes
} else {
response.addHeader( 'Content-Length', "${mp4Resource.contentLength()}")
response.outputStream << mp4Resource.inputStream
}
} else {
log.info 'no range, so responding with whole mp4'
response.addHeader( 'Content-Length', "${mp4Resource.contentLength()}")
response.outputStream << mp4Resource.inputStream
}
}
}
In the Safari console, I get:
Failed to load resource: Plug-in handled load
Nothing else. And sadly lots of fields in the web inspector are blank even though they're obviously set in the server.
I've tried so many things at this point that any help, pointers, tips will be appreciated. Thanks guys :) !
After trying many things and scouring many posts, this formula worked. You need all four of those headers. Don't need to return anything in the first request. This may not work for all browsers but this works for safari. Additional modifications can ensure all browsers are handled
class VideoController {
GrailsApplication grailsApplication
AssetResourceLocator assetResourceLocator
public index() {
Resource mp4Resource = assetResourceLocator.findAssetForURI('/../lol.mp4')
String range = request.getHeader('range')
if(range) {
String[] rangeKeyValue = range.split('=')
String[] rangeEnds = rangeKeyValue[1].split('-')
if(rangeEnds.length > 1) {
int startByte = Integer.parseInt(rangeEnds[0])
int endByte = Integer.parseInt(rangeEnds[1])
int contentLength = (endByte - startByte) + 1
byte[] inputBytes = new byte[contentLength]
def inputStream = mp4Resource.inputStream
inputStream.skip(startByte) // input stream always starts at the first byte, so skip bytes until you get to the start of the requested range
inputStream.read(inputBytes, 0, contentLength) // read from the first non-skipped byte
response.reset() // Clears any data that exists in the buffer as well as the status code and headers
response.status = 206
response.addHeader("Content-Type", "video/mp4")
response.addHeader( 'Accept-Ranges', 'bytes')
response.addHeader('Content-Range', "bytes ${startByte}-${endByte}/${mp4Resource.contentLength()}")
response.addHeader( 'Content-Length', "${contentLength}")
response.outputStream << inputBytes
}
}
}
}

How to send HTTP SOAP request to local Sonos device with NodeMCU?

How do I send a simple HTTP POST/GET SOAP request to my Sonos loudspeaker in Lua?
I have tried simple HTTP POST and GET requests with success, but I do not know where to start with SOAP requests.
Note: I am a newbie at this. I have never worked with a NodeMCU before nor have I programmed in Lua. I have experience in other languages though.
I know how to do it in C#, Java and PHP.
This works in Postman:
HTTP Headers:
SOAPAction:urn:schemas-upnp-org:service:AVTransport:1#Pause
Content-Type:text/xml; charset="utf-8"
Host:192.168.0.10:1400
BODY:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:Pause xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID></u:Pause></s:Body></s:Envelope>
What I did is this and it does not work:
sendRequest("192.168.0.10")
function sendRequest(url)
print("Sending request to Sonos Playbar...")
sk = net.createConnection(net.TCP, 0)
sk:on("receive", function(sck, c) print(c) end )
sk:on("connection", function(sck, c)
print("\r\n\r\n\r\n")
-- HTTP 405: Method not allowed
-- sck:send("POST / HTTP/1.1\r\nHost: "..url..":1400\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
-- HTTP 500, UPnP 402: Invalid arguments
-- sck:send("POST /MediaRenderer/AVTransport/Control HTTP/1.1\r\nHost: "..url..":1400\r\nSOAPAction:urn:schemas-upnp-org:service:AVTransport:1#Pause\r\nConnection: keep-alive\r\n\r\nAccept: */*\r\n\r\n")
local content = nil;
content = "POST /MediaRenderer/AVTransport/Control\r\n"
content = content.."Host:192.168.0.10:1400\r\n"
content = content.."Content-Type:text/xml; charset=utf-8\r\n"
content = content.."SOAPAction:urn:schemas-upnp-org:service:AVTransport:1#Pause\r\n"
content = content.."\r\n"
-- SOAP Body
content = content.."<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\""
content = content.." s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
content = content.."<s:Body>"
content = content.."<u:Pause xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\">"
content = content.."<InstanceID>0</InstanceID>"
content = content.."</u:Pause>"
content = content.."</s:Body>"
content = content.."</s:Envelope>"
-- SOAP Body End
print(content.."\r\n\r\n\r\n")
sck:send(content);
end)
sk:connect(1400, url)
end
I am getting this response of my Sonos player:
HTTP/1.1 500 Internal Server Error
CONTENT-LENGTH: 347
CONTENT-TYPE: text/xml; charset="utf-8"
EXT:
Server: Linux UPnP/1.0 Sonos/34.16-37101 (ZPS9)
Connection: close
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<s:Fault>
<faultcode>s:Client</faultcode>
<faultstring>UPnPError</faultstring>
<detail>
<UPnPError xmlns="urn:schemas-upnp-org:control-1-0">
<errorCode>401</errorCode>
</UPnPError>
</detail>
</s:Fault>
</s:Body>
</s:Envelope>
What am I doing wrong? I copied and paste the text, basically. Maybe it is the order of headers? Maybe I am declaring the headers wrong or something?
I don't have a Sonos device to play with. Thus, this ain't a confirmed answer.
The string in your content variable is not a valid HTTP request. Sonos doesn't understand it as the error code 401 means "invalid action".
You need the separate HTTP headers with \r\n. An extra \r\n needs to be placed right before the HTTP body. Therefore, I'd expect that your content should be:
"POST http://192.168.0.10:1400/MediaRenderer/AVTransport/Control\r\n
SOAPAction:urn:schemas-upnp-org:service:AVTransport:1#Pause\r\n
Content-Type:text/xml; charset=\"utf-8\"\r\n
Host:192.168.0.10:1400\r\n\r\n
<s:Envelope xmlns:s=\"http://schemas.xml......"
Finally! I have it working! Below is the code to get it working:
sendRequest("192.168.0.10")
function sendRequest(url)
print("Sending request to Sonos Playbar...")
local content = nil;
content = "";
-- SOAP Body
content = content.."<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\""
content = content.." s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
content = content.."<s:Body>"
content = content.."<u:Pause xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\">"
content = content.."<InstanceID>0</InstanceID>"
content = content.."</u:Pause>"
content = content.."</s:Body>"
content = content.."</s:Envelope>"
-- SOAP Body End
http.post("http://"..url..":1400/MediaRenderer/AVTransport/Control",
'Content-Type: text/xml; charset="utf-8"\r\n'..
'Host:'..url..':1400\r\n'..
'SOAPAction:urn:schemas-upnp-org:service:AVTransport:1#Pause\r\n',
content, function(code, data)
if(code < 0) then
print("HTTP request failed with code "..code)
else
print(code, data)
end
end)
end

Identity Server OAuth Resource Owner Password Grant always returns invalid_client

new Client
{
ClientId = "esmifavorito",
ClientName = "esmifavorito-client",
Enabled = true,
ClientSecrets = new List<ClientSecret>
{
new ClientSecret("esmifavorito".Sha256()) //PQ/pIgjXnBfK67kOxGxz9Eykft6CKPkPewR3jUNEkZo=
},
Flow = Flows.ResourceOwner,
//RequireConsent = false,
//AllowRememberConsent = false,
//ClientUri = "http",
RedirectUris = new List<string>
{
"https://localhost:44304",
},
ScopeRestrictions = new List<string>
{
},
AllowedCorsOrigins = new List<string>
{
"https://localhost:44304",
"http://localhost:50655",
"chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm",
"*",
},
PostLogoutRedirectUris = new List<string>
{
"https://localhost:44304",
},
AccessTokenType = AccessTokenType.Jwt,
IdentityTokenLifetime = 3000,
AccessTokenLifetime = 3600,
AuthorizationCodeLifetime = 300
}
I have registered my client, with implicit flow it works but I need to implement a login form so I'm trying Resource owner password credentials grant.
I'm doing requests to the endpoint with Postman in Chrome (that's why I added the chrome-extension to CORS, just to see if that was the error...)
I've tried a lot of requests (using https)
POST /connect/token HTTP/1.1
Host: localhost:44302
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded
grant_type=password&username=test&password=testuser&client_id=esmifavorito
-
POST /connect/token HTTP/1.1
Host: localhost:44302
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded
grant_type=password&username=test&password=testuser&client_id=esmifavorito&client_secret=PQ%2FpIgjXnBfK67kOxGxz9Eykft6CKPkPewR3jUNEkZo%3D
-
POST /connect/token HTTP/1.1
Host: localhost:44302
Authorization: Basic ZXNtaWZhdm9yaXRvOlBRL3BJZ2pYbkJmSzY3a094R3h6OUV5a2Z0NkNLUGtQZXdSM2pVTkVrWm89
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded
grant_type=password&username=test&password=testuser
Those should have worked but I'm always getting invalid_client
The error log is empty, I don't know if I have done the tracer registration right
LogProvider.SetCurrentLogProvider(new DiagnosticsTraceLogProvider());
app.UseIdentityServer(new IdentityServerOptions
{
LoggingOptions = new LoggingOptions {
IncludeSensitiveDataInLogs = true,
WebApiDiagnosticsIsVerbose = true,
EnableWebApiDiagnostics = true,
//EnableHttpLogging = true
},
SiteName = "Thinktecture IdentityServer3 - UserService-AspNetIdentity",
SigningCertificate = Certificate.Get(string.Format(#"{0}\bin\IdentityServer\IdentityServerEMFDev.pfx", AppDomain.CurrentDomain.BaseDirectory), "KG0yM0At"),
Factory = idSvrFactory,
CorsPolicy = CorsPolicy.AllowAll,
AuthenticationOptions = new AuthenticationOptions
{
IdentityProviders = ConfigureAdditionalIdentityProviders,
},
}
);
With this in web.config
<trace autoflush="true"
indentsize="4">
<listeners>
<add name="myListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="Trace.log" />
<remove name="Default" />
</listeners>
</trace>
The client data is correct since I have succesfuly logged in with implicit flow.
What am I missing? This is getting on my nerves, I'm reading the OAuth RFC and I don't see why this shouldn't work.
I tried the new version of Postman (I don't know its number, but now it runs on the desktop as a chrome app), I copied the values from the old Postman version and now everything works.
POST /connect/token HTTP/1.1
Host: localhost:44302
Authorization: Basic ZXNtaWZhdm9yaXRvOmVzbWlmYXZvcml0bw==
Cache-Control: no-cache
Postman-Token: fc4acc63-29f2-6a37-b92c-b62034b13c29
Content-Type: application/x-www-form-urlencoded
grant_type=password&username=test&password=testuser&scope=write
This is the resulting request.
In Postman 1 I have the same thing (excluding the Postman-Token) and it gives me invalid_client. I even used a similar Firefox tool with the same results.
I don't know how is this possible.
Could it be something with the chrome-extension://?
I'm gonna answer myself but if someone knows what is happening here I'll be eternally grateful.
It seems per this article that Authorization must be sent in the header
https://github.com/IdentityServer/IdentityServer3/issues/1520
In my case I had the same problem and I noticed it was due to my HttpClient setting a "custom" authorization header.
If using IdentityModel to request the resource owner password token, notice the Authorization header must contain client_id:client_secret in base 64.
In my case I was setting a different authorization header and, although the body values were correct, the IResourceOwnerPasswordValidator was not even attempting to validate the request.

JQuery consuming ASP.Net Web Service webserver - Request format is unrecognized for URL unexpectedly ending in

Done a lot of Googling on this but cant seem to find an answer.
When I call my web service from Jquery I am receiving the error
Request format is unrecognized for URL unexpectedly ending in '/AirportSearchGeneric'.
Factors
I am currently calling a webservice that is on the same machine but on a different webserver (calling app is port 64004 and receiving app is 1400) - possible cross "domain" issue? Both are local host.
Both are using the test web server that is part of visual studio.
I have tried adding the 2 protocols to the web.config (add name="HttpGet" add name="HttpPost")
The error occures in the Event Viewer on the server.
I get the following in Firebug...
OPTIONS AirportSearchGeneric
http://localhost:1400/services/airportservice.asmx/AirportSearchGeneric
500 Internal Server Error
localhost:1400
... not seen OPTIONS before but the request is being accessed with a POST request.
JQuery code...
$.ajax({
type: "POST",
url: "http://localhost:1400/services/airportservice.asmx/AirportSearchGeneric",
data: "{'criteria':'EGBB', 'maxResults':'10'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg.d);
}
});
Web service code...
[WebService(Namespace = "http://localhost/WebServices")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class AirportService : WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public string AirportSearchGeneric(string criteria, int maxResults)
{
IAirportService svc = new Airports.AirportService.AirportService();
List<AirportSearchResult> res = svc.AirportSearchGeneric(criteria, maxResults);
DataContractJsonSerializer serializer = new DataContractJsonSerializer(res.GetType());
MemoryStream ms = new MemoryStream();
serializer.WriteObject(ms, res);
string jsonString = Encoding.Default.GetString(ms.ToArray());
ms.Close();
return jsonString;
}
}
... dont think its a problem in here as when debugging, no code in here gets executed.
Pretty sure I have covered off all of the reasons I have read as to why this occurs so would be greatful for any advice on how I can get this working.
Cheers.
For reference the firebug headers are as follows:
Host localhost:1400
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12 ( .NET CLR 3.5.30729; .NET4.0E)
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language en-gb,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 115
Connection keep-alive
Origin http://localhost:64004
Access-Control-Request-Me... POST
(No response is received in firebug apart from the 500 error, there is no html response at all).
Using different ports on the same machine is considered cross domain and not allowed by the browser as you were suspecting.
You either call the other port by tricking the browser using JSONP (if can limit yourself to only using GET requests) or change one of the ports to be the same as the other.
Ajax Cross Domain Calls

Resources