Home > Article > Web Front-end > Cross-domain requests: jQuery’s ajax jsonp usage solution
Directly executing the error method prompts an error - ajax jsonp has not been used before, and the understanding of it is similar to that of ordinary ajax requests, and there is no in-depth understanding of it; this error occurred, after debugging several times (check the background code and js part of the property settings) still doesn't work, which makes me feel very surprised and puzzled. Therefore, I decided to carefully study the use of ajax jsonp, and share the learning experience of the final successful test with everyone!
First, post the code that can be successfully executed:
(page part)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Untitled Page</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script type="text/javascript"> jQuery(document).ready(function(){ $.ajax({ type : "get", async:false, url : "ajax.ashx", dataType : "jsonp", jsonp: "callbackparam",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(默认为:callback) jsonpCallback:"success_jsonpCallback",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名 success : function(json){ alert(json); alert(json[0].name); }, error:function(){ alert('fail'); } }); var a="firstName Brett"; alert(a); }); </script> </head> <body> </body> </html>
(Handler part)
<%@ WebHandler Language="C#" Class="ajax" %> using System; using System.Web; public class ajax : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; string callbackFunName = context.Request["callbackparam"]; context.Response.Write(callbackFunName + "([ { name:\"John\"} ] )"); } public bool IsReusable { get { return false; } } }
ajax request parameter description:
dataType String
The data type expected to be returned by the server. If not specified, jQuery will automatically make intelligent judgments based on the MIME information of the HTTP package. For example, the XML MIME type is recognized as XML. In 1.4, JSON will generate a JavaScript object, and script will execute the script. The data returned by the server will then be parsed based on this value and passed to the callback function. Available values:
"xml": Returns an XML document that can be processed with jQuery.
"html": Returns plain text HTML information; the included script tag will be executed when inserted into the dom.
"script": Returns plain text JavaScript code. Results are not cached automatically. Unless the "cache" parameter is set. '''Note:''''When making remote requests (not under the same domain), all POST requests will be converted into GET requests. (Because the DOM script tag will be used to load)
"json": Returns JSON data.
"jsonp": JSONP format. When calling a function using JSONP format, such as "myurl?callback=?" jQuery will automatically replace ? with the correct function name to execute the callback function.
"text": Returns a plain text string
jsonp String
Rewrite the name of the callback function in a jsonp request. This value is used to replace the "callback" part of the URL parameter in a GET or POST request such as "callback=?". For example, {jsonp:'onJsonPLoad'} will cause "onJsonPLoad=?" to be passed to the server.
jsonpCallback String
Specify a callback function name for the jsonp request. This value will be used instead of the random function name automatically generated by jQuery. This is mainly used to allow jQuery to generate unique function names so that it is easier to manage requests and provide callback functions and error handling. You can also specify this callback function name when you want the browser to cache GET requests.
The main difference between ajax jsonp and ordinary ajax requests is the processing of request response results. The response result shown in the above code is:
success_jsonpCallback([ { name: "John"} ] ); ——In fact, it is to call the jsonp callback function success_jsonpCallback and pass the string or json to be responded to here Method (as a parameter value), its underlying implementation, a rough guess should be:
function success_jsonpCallback(data) { success(data); }
For more cross-domain requests on the use of jQuery's ajax jsonp and related articles, please pay attention to the PHP Chinese website!