in reference to this link Ajax json post to Controller across domains, "not allowed by" Access-Control-Allow-Headers I am trying to do the same. So I have got on one part my html page(SPA) hosted on a apache server here the code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<meta name="apple-mobile-web-app-capable" content="yes" />
<title>MyMobilePage</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
<!-- Librerie PhotoSwipe -->
<link rel="stylesheet" type="text/css" href="../PhotoSwipe/photoswipe.css">
<link rel="stylesheet" type="text/css" href="../PhotoSwipe/styles.css">
<script type="text/javascript" src="../PhotoSwipe/klass.min.js"></script>
<script type="text/javascript" src="../PhotoSwipe/code.photoswipe-3.0.5.min.js"></script>
<!-- End PhotoSwipe -->
In the same page here my call to my ASPX
<script type="text/javascript">
$(document).ready(
function(){
alert("I am in!")
$.ajax({
url: 'http://publicIP:Portn/GetNames.aspx/AddPerson',
jsonp: 'callback',
dataType: 'jsonp',
data: { firstName: 'john', lastName: 'smith' },
success: function (result) {
alert(result);
}
}); } );
</script>
in my ASPX codebihind page insted:
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Web.Script.Serialization
Imports System.Web.mvc
Public Class JsonpResult
Inherits ActionResult
Private ReadOnly _obj As Object
Public Sub New(obj As Object)
_obj = obj
End Sub
Public Overrides Sub ExecuteResult(context As ControllerContext)
Dim serializer = New JavaScriptSerializer()
Dim callbackname = context.HttpContext.Request("callback")
Dim jsonp = String.Format("{0}({1})", callbackname, serializer.Serialize(_obj))
Dim response = context.HttpContext.Response
response.ContentType = "application/json"
response.Write(jsonp)
response.End()
End Sub
Public Function AddPerson(person As String) As ActionResult
Return New JsonpResult(True)
End Function
End Class
First question do I have to write something in the HTML of this ASPX?
Here my IIS configuration to allow HttpResponse (Web.config) - I guess I put too many stuff in it, could you tell me which onese I can remove, my aim is to run this example but than I want to return an HTML code I don't know how I guess with JSONP but this is next step for me now I want to make this example working.
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="SOAPAction, application/json, Origin, X-Requested-With, Content-Type, Accept, Authorization" />
<add name="Access-Control-Allow-Methods" value="POST, PUT, DELETE, GET, OPTIONS" />
</customHeaders>
</httpProtocol>
So when I run from my browser chrome Version 31.0.1650.63 m I got this debugging the "Network" with chrome
Request URL:http://publicIP:Portn/GetNames.aspx/AddPerson?callback=jQuery1910335388598497957_1387526286364&firstName=john&lastName=smith&_=1387526286365
Request Method:GET
Status Code:500 Internal Server Error
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,it;q=0.6
Cache-Control:max-age=0
Connection:keep-alive
Cookie:__utma=155182590.733648325.1387385626.1387385626.1387406760.2; __utmz=155182590.1387385626.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
Host:publicIP:Portn
Referer:http://www.mywebsite.com/Test/Test.html
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
Query String Parametersview sourceview URL encoded
callback:jQuery1910335388598497957_1387526286364
firstName:john
lastName:smith
_:1387526286365
Response Headersview source
Access-Control-Allow-Headers:SOAPAction, application/json, Origin, X-Requested-With, Content-Type, Accept, Authorization
Access-Control-Allow-Methods:POST, PUT, DELETE, GET, OPTIONS
Access-Control-Allow-Origin:*
Cache-Control:private
Content-Length:3420
Content-Type:text/html; charset=utf-8
Date:Fri, 20 Dec 2013 07:58:19 GMT
Server:Microsoft-IIS/8.5
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
I get the error message 500 Internal server error, what can be, did I forget something to config.
Thank you in advance.